Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Ranging Case Values in Switch Construct

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Ranging Case Values in Switch Construct


    Is there any way to use a range of numbers as a case value? For example, I need numbers 0 through 59 to perform the same operation. I know it would be easier to use an if statement, but the assignment calls for a switch. Is there any way to write it as:

    public void range(int number) {
    switch(number) {
    case 0 - 59:
    yadda yadda yadda
    }
    }

    Rather than having to code it out case by case like:

    public void range(int number) {
    switch(number) {
    case 0:
    case 1:
    blah blah blah
    }
    }

    I have 100 values to go through in chunks of 10 (except for the 0 through 59). I'm guessing there's no way of doing it, but any help would be appreciated!









  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Ranging Case Values in Switch Construct

    Not without a lot of copy/pasting. if statements are for ranges, switch statements are for specific values.
    see Switch statements

Similar Threads

  1. Array with Switch Construct
    By WhiteSoup12 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 7th, 2011, 01:41 AM
  2. CompareToIngnore case problem
    By newTaz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 7th, 2010, 05:28 PM
  3. wanting to convert printout to UPPER case
    By welikedogs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 3rd, 2010, 01:22 PM
  4. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 03:15 PM
  5. [SOLVED] upper case
    By andaji in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2010, 11:54 PM