Results 1 to 10 of 10
  1. #1
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default How to use Greater Than and Less than in a switch statement

    Is there anyway to use greater than or less than in Switch statements?

    Like this

    switch(variable){
    case <0:
    do stuff;
    break;
    default:
    break;
    }

    Thanks for any help

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    thats crazy, just use an if-else statement.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default

    hi,

    as far as I know only constants are in switch-case-statements allowed. I would also go for if-else
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

  4. #4
    JordashTalon is offline Member
    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    0

    Default

    I did use if else statements at first and it worked fine, the only problem is in my class they use this thing called Checkstyle and the more if else statements I have it gives a Cyclomatic Complexity error. Has anyone here used Checkstyle? Checkstyle is alot more forgiving on Switch Statements.

  5. #5
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default

    hm, never herd of that error. have u some code beside that code above?
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

  6. #6
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    dont know if it will help but here is a switch statement that I have in a working app

    Java Code:
    // THE SWITCH BELOW WILL GIVE A DISCOUNT FOR THE NUMBER OF YEARS NO CLAIMS DISCOUNT
    switch (Integer.parseInt(txtYears.getText())) {  
           case 1:
               premium = premium * 0.80;
               break;
            case 2:
               premium = premium * 0.70;
               break;
            case 3:
               premium = premium * 0.60;
               break;
            case 4:
               premium = premium * 0.50;
               break;
            case 5:
               premium = premium * 0.45;
               break;
    // 2 years no claims bonus case 6
            case  6:
               premium = premium * 0.41;
               break;
            case  7:
               premium = premium * 0.40;
               break;
               
           default:
               premium = premium ;
       }

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    I would put the lookup into a static HashMap<Integer, Double> in this way:
    Java Code:
    static Map<Integer, Double> factors = new HashMap<Integer, Double>();
    factors.put(1, 0.80);
    factors.put(2, 0.70);
    factors.put(3, 0.60);
    ... ...
    :
    :
    premium *= factors.get(Integer.parseInt(txtYears.getText()));
    This has the advantage of keeping the lookup values at the top (or bottom) of the class, easy to find if the business rules change.

    Of course, if frequent changes are anticipated, the data should be read from a database.

    db

  8. #8
    Nomadyn is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Re: How to use Greater Than and Less than in a switch statement

    Quote Originally Posted by JordashTalon View Post
    Is there anyway to use greater than or less than in Switch statements?

    Like this

    switch(variable){
    case <0:
    do stuff;
    break;
    default:
    break;
    }

    Thanks for any help
    Hi! I was looking for the answer to this myself, and I think I discovered an easy way as I was reading.

    add an int.

    int i;

    if(var1>var2) i = 1;
    if(var1=var2 i = 0;
    if(var1<var2) i = -1;

    switch (i);
    {
    case -1:
    do stuff;
    break;

    case 0:
    do stuff;
    break;

    case 1:
    do stuff;
    break;

    }

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to use Greater Than and Less than in a switch statement

    If it's just a few small ranges, you could take advantage of the fact that execution "falls through" to the next case whenever you omit a break statement:

    Java Code:
    switch(someInteger) {
       case 1:
       case 2:
       case 3:
          // do stuff
       break;
       case 4:
       case 5:
       case 6:
          // do stuff
       break;
    }
    If there are many large ranges, an if-else statement would probably be better. Switches can be more efficient, but they are harder to write and maintain, and therefore more likely to introduce bugs. I only use large switches in generated code.
    Get in the habit of using standard Java naming conventions!

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: How to use Greater Than and Less than in a switch statement

    Last edited by DarrylBurke; 06-17-2012 at 04:16 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 02:22 PM
  2. Demonstration of the switch statement
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  3. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 03:48 AM
  4. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  5. Help with gigantamous switch statement
    By trill in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •