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 3 of 3

Thread: I need to make cases in a switch statement over certain ranges

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need to make cases in a switch statement over certain ranges

    Here is the half code / half psuedocode of what I need to make.
    I figured that I should use logical operators, but I couldn't find a way to make it work.

    double gross;
    double taxPercent;
    switch(gross)
    {
    	case 0 to 300.00:
    		taxPercent = 0.10;
    		break;
     
    	case 300.01 to 400.00:
    		taxPercent = 0.12;
    		break;
     
    	case 400.01 to 500.00:
    		taxPercent = 0.15;
    		break;
     
    	case > 500.01:
    		taxPercent = 0.20;
    		break;
     
    	default:
    	System.out.println("ERROR! gross pay is less than 0");
    	break;
    }

    I'd like to only use the switch statement for this.
    Last edited by davis220; March 20th, 2012 at 08:32 PM.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: I need to make cases in a switch statement over certain ranges

    The switch construct can only check for constants, if you want to check for a range, you should use if then structure instead.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: I need to make cases in a switch statement over certain ranges

    Use an if/else-if chain.

    I was going to offer the facetious alternative of having a *lot* of case parts. In fact, that won't fly. Switches are meant for when the thing being switched upon represents one out of a number of distinct possibilities. Integers are like that, as are characters: they are distinct. Floating point quantities like doubles represent things measured rather than counted - and they sort of "blend" into one another. In any case, you can't switch on a double in Java.

    Of course, you could work in an int number of cents. Then I could make the facetious suggestion.

    But your code suggests that you are really thinking of switching on something else: the "tax bracket". These are distinct and you can use them with switch/case providing you represent the various tax brackets as enums. I'm not sure this will completely free you from using an if/else-if chain though.

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 06:19 PM
  2. [SOLVED] (beginner) Switch Statement
    By georgewbw in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2011, 12:18 AM
  3. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 02:23 PM
  4. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 7
    Last Post: July 19th, 2011, 07:25 AM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 01:52 PM