Skip to content

Errorin update variable [closed]

i want update my variable and my code has a error that i dont khow where i have error

    String name = " "; 
    String family = " "; 
    int age = 0;
    Scanner input = new Scanner(;

    int choise;
    while (true){
        System.out.println("1:Name  2:Family  3:Age ---- 0:Exit");
        choise = input.nextInt();
        if (choise == 0) break;
        else if (choise == 1){
            System.out.println("Please enter the name : ");
            name = input.nextLine();
        else if (choise == 2){
            System.out.println("Please enter the family : ");
            family = input.nextLine();
        else if (choise == 3){
            System.out.println("Please enter the age : ");



I was trying to run your code in my local and I found 2 issues there:

  1. When application try to wait for input in if-else part, it’s not waiting instead of loop to next iteration. enter image description here
  1. Relate to no 1 and based on the result, application should be waiting for entering name. After I enter my name, it thrown an error. Why? Clearly that the application is not waiting for name, instead of choice.

The issue is clearly describe here: Java Scanner doesn’t wait for user input

The problem is that nextInt() does not consume the ‘n’, so the next call to nextLine() consumes it and then it’s waiting to read the input for y

My suggestion is change nextInt() to nextLine() then convert to int manually

choise = Integer.parseInt(input.nextLine());
User contributions licensed under: CC BY-SA
5 People found this is helpful