2

I tried to make a multiple random pick ups that would print the message for each input number, i.e. serie of numbers (4 2 17 0) where 0 will stop the code. Got wrong output


public class Main {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int number=scanner.nextInt();
       
  int x = number;
do{switch(x) {

case 1:
      System.out.println("Language selection");
break;
case 2:
       System.out.println("Customer support");
break;
case 3:
       System.out.println("Check the balance");
break;

case 4:
       System.out.println("Check loan balance");
break;
case 0:
System.out.println("Exit");
break;
  
default:
return;

}
if (x==0)
{break;}
x++;
}

while (true);

   }
} ```
![enter image description here](https://i.sstatic.net/RJBYn.jpg)

![enter image description here](https://i.sstatic.net/wCm4p.jpg)
6
  • You need a break in every case statement, otherwise they'll just fall through. And then you need better logic to exit the loop (like either a label or set a boolean and check that instead of using while(false)) Commented Aug 21, 2021 at 9:09
  • Thank you, tried now with break, didn’t help. I have to use do while as a part of exercise Commented Aug 21, 2021 at 9:18
  • Also, while(false) should really be while(true), otherwise you will exit your loop immediately after the first iteration, irrespective of what the rest of the code does. (while loops if its condition is true, not false) Commented Aug 21, 2021 at 9:30
  • With while(true) got “Execution time out” for each test Commented Aug 21, 2021 at 9:34
  • I just noticed that the do...while loop basically does nothing. Shouldn't you have the switch inside the loop? Anyway, if you're having difficulty with this you should probably re-read whatever material you're using to learn java. Because I know the next question will be about the loop going on forever again because you'll need an exit condition for the loop. Instead, re-study how loops, conditions and switch work, reason about what your program is doing and try to re-write it. Commented Aug 21, 2021 at 9:38

1 Answer 1

2

You can use Scanner#hasNextInt to terminate the loop when integers are exhausted.

Demo:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()) {
            int x = scanner.nextInt();
            switch (x) {
            case 1:
                System.out.println("Language selection");
                break;
            case 2:
                System.out.println("Customer support");
                break;
            case 3:
                System.out.println("Check the balance");
                break;
            case 4:
                System.out.println("Check loan balance");
                break;
            case 0:
                System.exit(0);
            }
        }
    }
}

A sample run:

4 2 17 0
Check loan balance
Customer support

Also, note that you need break to avoid the cases falling through inadvertently. I did not see any use of default in this case and therefore, I have removed it from the demo code.

Alternatively, using a do-while loop:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = 0;

        do {
            x = scanner.nextInt();
            switch (x) {
            case 1:
                System.out.println("Language selection");
                break;
            case 2:
                System.out.println("Customer support");
                break;
            case 3:
                System.out.println("Check the balance");
                break;
            case 4:
                System.out.println("Check loan balance");
                break;
            }
        } while (x != 0);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That’s brilliant ! Thank you! It works! 👌``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()){ int x=scanner.nextInt(); switch(x) { case 1: System.out.println("Language selection"); break; case 2: System.out.println("Customer support"); break; case 3: System.out.println("Check the balance"); break; case 4: System.out.println("Check loan balance"); break; case 0: System.out.println("Exit"); System.exit(0); }}}}```

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.