0

I'm trying to use switch statements in a while loop in Java, but there is something going wrong. Please have a look at a sample code below which explains my problem:

Scanner input=new Scanner(System.in);
    int selection = input.nextInt();

while (selection<4)
      {  switch(selection){
            case 1:
               System.out.println("Please enter amount");
               double amount=input.nextDouble(); //object of scanner class
               break;

            case 2:
               System.out.println("Enter ID number"); 
               break;

            case 3:
               System.out.println("Enter amount to be credited");
               break;
                          }
System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
     }

If I run this code, the output is as follows:

1
Please enter amount
2000
1. Transfer
2.Check balance
3.Recharge
Please enter amount
2
1. Transfer
2.Check balance
3.Recharge
Please enter amount

When I enter the amount, I would then like to choose another option - and the output should be according to the option chosen (you should probably be knowing what I want this code to do). Could someone please help correct the code?

Thanks

1
  • 1
    you prompt for a value, then never prompt again. you just keep looping on the SAME value you'd prompted for at the beginning. Commented Aug 14, 2015 at 21:02

3 Answers 3

3

You currently get and set the selection value once and before the while loop, and so there is no way to change this from within the loop. The solution: Get your next selection value from the Scanner object inside of the while loop. To understand this, think the problem out logically and be sure to walk through your code mentally and on paper as the issue is not really a programming issue but rather a basic logic issue.


Regarding:

Could someone please help correct the code?

Please don't ask us to do this and for several reasons.

  1. This is not a homework completion service
  2. You're harming yourself by asking others to change the code for you, as you learn how to code by writing code.
  3. Really this is a basic simple issue that you have the ability to fix on your own. Please give it a try, and only if the attempt doesn't work, then show us your attempt.
Sign up to request clarification or add additional context in comments.

1 Comment

1. The homework question was to create a whole program (way more complicated than this, seriously), which I did. 2. I didn't mean to ask anyone to change my code. I just needed guidance. It's probably my language. Sorry about that 3. Thanks for your help :)
2

You're forgetting to ask for the selection again. It's not going to change once it's been entered.

Scanner input=new Scanner(System.in);
int selection = input.nextInt();

while (selection<4)
{
   switch(selection){
        case 1:
           System.out.println("Please enter amount");
           double amount=input.nextDouble(); //object of scanner class
           break;

        case 2:
           System.out.println("Enter ID number"); 
           break;

        case 3:
           System.out.println("Enter amount to be credited");
           break;
      }
      System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
      selection = input.nextInt(); // add this
 }

You could even use a do...while loop instead to avoid writing input.nextInt(); twice

Scanner input=new Scanner(System.in);
int selection;

do
{
   selection = input.nextInt();
   switch(selection){
        case 1:
           System.out.println("Please enter amount");
           double amount=input.nextDouble(); //object of scanner class
           break;

        case 2:
           System.out.println("Enter ID number"); 
           break;

        case 3:
           System.out.println("Enter amount to be credited");
           break;
      }
      System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
 }
 while(selection < 4);

1 Comment

Silly me! Thanks for figuring that out
-1

Case must be bigger than 4, in your case the cases are less than 4. so you won't quit the loop, basically the break statement breaks the switch and jumps to loop, but than the loop is again less than 4 so it jumps again into the switch and so on. Fix the Sizes of your cases, maybe just make an

(selection != 1 || selection != 2 || selection !=3 || selection !=4)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.