-2

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(System.in);


    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 : ");
        }
    }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
ali pasha
  • 168
  • 1
  • 2
  • 9
  • 2
    What is the error? – tomerpacific Jun 15 '21 at 09:38
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – maloomeister Jun 15 '21 at 09:41
  • 2
    "i dont khow where i have error" - we don't either since we don't know what your code is _supposed_ to do, i.e. what you expect, and what it is doing instead. – Thomas Jun 15 '21 at 09:41
  • What exactly you are trying to do? what is the expected behavior? – Ruchira Gayan Ranaweera Jun 15 '21 at 09:42
  • the error is : Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Code_4.main(Code_4.java:14) @tomerpacific – ali pasha Jun 15 '21 at 09:58

2 Answers2

0

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());
rioswarawan
  • 117
  • 1
  • 4
-1

i change code to this and it is working now. but shoud i write input.nexline() or input.nexint() after name = input.nexline() or ..?

 String name = " "; String family = " "; int age = 0;
    Scanner input = new Scanner(System.in);


    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();
            input.nextLine();
        }
        else if (choise == 2){
            System.out.println("Please enter the family : ");
            family = input.nextLine();
            input.nextLine();
        }
        else if (choise == 3){
            System.out.println("Please enter the age : ");
            age = input.nextInt();
            input.nextInt();
        }
    }
ali pasha
  • 168
  • 1
  • 2
  • 9