简体   繁体   中英

Scanner won't wait to read nextInt() in loop

I need to read in an integer then display a message if it was not an integer, I need this to loop until only a int is entered. The problem I have with the code below is when it loops it won't wait to read the nextInt, it will just keep looping - printing out the try again message.

    do {
        if (reader.hasNextInt()) {
            userX = reader.nextInt();
            isANumberFlag = true;
        } else {
            System.out.println("Please enter numbers only, try again: ");
        }
    } while (isANumberFlag == false);

You are stuck in the while loop because the index of your reader stays at the same point.
So if you type "some non numeric gibberish" the pointer will stay at this point and the program will keep asking you to enter a number. You can solve this by either moving your index in the else clause:

 Scanner reader = new Scanner(System.in);
    boolean isANumberFlag = false;
    int userX = 0;
    do {
      System.out.println("please enter a number: ");

      if (reader.hasNextInt()) {
        userX = reader.nextInt();
        isANumberFlag = true;
      }
      else {
        System.out.println("Please enter numbers only, try again: ");
        reader.next(); //move your index in the else clause
      }
    }
    while (isANumberFlag == false);
    System.out.println(userX);
  }

Note this solution will ask you to type something again if you type a word that is partial numeric, for instance: "notValid5000" .

However if you type a partial numeric sentence, for instance: "notValid 5000" it will say "Please enter numbers only, try again: " and directly accept the numeric 5000 part, as an Integer.

Another solution is reading a line as a String and validating that the specific line is a int by using a regular expression:

Scanner reader = new Scanner(System.in);
    boolean isANumberFlag = false;
    String input;
    int userX = 0;
    do {
      System.out.println("please enter a number: ");
      input = reader.nextLine();

      if (input.matches("\\d+")) {
        userX = Integer.parseInt(input);
        isANumberFlag = true;
      }
      else {
        System.out.printf("you typed %s, which is not allowed \n", input);
        System.out.println("Please enter numbers only, try again: ");
      }
    }
    while (isANumberFlag == false);
    System.out.println(userX);

This solution will only accept your input, if the entire line is numeric.

More information regarding int validation with a Scanner can be found on this topic

I would do something like below. You need to create 2 Scanner object, first one to get user input, and the second one to verify and get the int.

    Scanner sc = new Scanner(System.in);
    boolean isANumberFlag = false;
    int userX = 0;
    do {
        Scanner input = new Scanner(sc.nextLine());
        if (input.hasNextInt()) {
            userX = input.nextInt();
            isANumberFlag = true;
        } else {
            System.out.println("Please enter numbers only, try again: ");
        }
    } while (!isANumberFlag);
}

If the condition for your loop is !(reader.hasNextInt()) then why would if (reader.hasNextInt()) even run? Those statements contradict each other. One says it doesn't haven't a value and one says it does. Try changing your if statement's condition to !(reader.hasNextInt()) or use a Boolean variable to control the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM