0

Noob here. I am trying to get a loop going where if an user enter something that is not a number gets an error and can repeat until he gets it right. I somehow end up in an infinte loop with the following code:

public static void console(){
Scanner scanner = new Scanner(System.in);
int input = 1;
int base = 1;
String welcomeMessage = "Enter a positive integer and either base%n";
String choice = "y";
while (!choice.equalsIgnoreCase("n")){
    try{
    System.out.print("Enter an integer: ");
    input = scanner.nextInt();   
    System.out.println(input);     
    System.out.print("Enter an base (2, 8, or 16): ");
    base = scanner.nextInt();
    System.out.println(base);
    }catch(InputMismatchException e){
      //  System.out.println("you suck");
       continue;

    }
}

}
  • 1
    your value of `choice` is always `"y"` so the loop is not terminated – Md Johirul Islam Apr 22 '20 at 20:36
  • This code is incomplete, you are not processing the input, nor you actually request user if he/she wants to try again (where you should request "y" or "n" and assign that to the `choice` – Dmitry Pisklov Apr 22 '20 at 20:37

0 Answers0