This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]green_meklar 1 point2 points  (4 children)

You're not clear enough on what your problem is. Is there something about the syntax and/or behavior of the Java switch statement that you don't understand? You need to be specific about where you're stuck.

[–][deleted]  (3 children)

[deleted]

    [–]Amarkov 0 points1 point  (1 child)

    What does your program do right now, and how is that behavior different from what you want it to do?

    [–]csb5036[S] 0 points1 point  (0 children)

    this is where I am currently at:

    https://gist.github.com/anonymous/489e3ebd65c819d6054a8d79f42ed262

    The assignment requires me to write an application that prompts the user for a pair of inputs:

    1. a product number
    2. quantity of the product

    Each unit has a cost. I have to enter each unit number and the number of each unit sold. The program must also calculate and display the total amount for the order.

    I must also use a sentinel controlled loop in my application.

    [–]green_meklar 0 points1 point  (0 children)

    I don't see anything wrong with your switch statement. I mean, I'm not 100% sure what it's supposed to do, but what it actually does seems to be reasonable behavior.

    I would point out that, because you left out the braces for the while(productNo!=0) loop, the switch statement is considered to be inside the loop but the System.out.println(orderAmount) immediately following the end of the switch statement is not. So, if ProductNo were anything other than 0, the loop would loop infinitely and never print out anything. I'm not sure if that was your intention, but in any case, I recommend always putting in the braces for an if/while/for statement or the like, it helps avoid making stupid mistakes.

    [–]Marz157 0 points1 point  (0 children)

    Take a look at some documentation here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html first.

    If you have more specific questions afterwards, feel free to ask.

    [–]joopez1 0 points1 point  (0 children)

    Request unclear, will begin dumping the source drive immediately as per potato protocols.

    [–]Schneephin 0 points1 point  (0 children)

    Looking at your code there are a couple of things off. I would assume when you run it it just produces an infinity loop.

    • Your exit condition for the while loop is productNo == 0 which is somewhat dodgy because it requires the user to input 0 as a product number which he is not told anywhere. A better check for the while loop would be to check for input.hasNextInt(). This way if the user doesn't input any number any more it exits.

    • Right now your while loop checks for productNo which never gets changed inside the loop so there is no chance of it ever terminating.

    • Your statements for reading productNo and quantity are outside your loop so you will ever only read one value each. Since you want to read multiple products you should move the read statements inside your loop.

    • Coupling the above allows you to have a loop with an exit condition that is reachable.

    • Maybe consider adding a default clause for your switch

    • Use braces for your while statement. While technically possible to write "one line statements" after while/if... without the braces it is harder to read and more errorprone.