Using Scanner to validate user input

两盒软妹~` 提交于 2019-12-11 21:36:27

问题


I have a hard time to figure out how to use the Scanner correctly to validate the user input. For example, the program need to have a user input of int, user can only input positive number, and if the user input "apple","delicious apple",or negative number, the problem will show error message. I have tried the code below, and then i found a disturbing questions, the "That's not a number!" are printed twice, I have no idea what caused this questions....

import java.util.*;
public class input {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        int number;

        do {
            System.out.println("Please enter a positive number!");
            while (!sc.hasNextInt()) //scan
            {
                System.out.println("That's not a number!");
                sc.nextLine();  //scan
            }
            number = sc.nextInt();

        }
        while (number <= 0);

        System.out.println("Thank you! The positive number is " + number);
    }
}

Here is the result:
Please enter a positive number!
dfd
That's not a number!
-2232
Please enter a positive number!
dfd dfd
That's not a number!
That's not a number!

And I accidently solve the issue by putting extra line of code below the "number = sc.nextInt()", and now my code became:

import java.util.*;

public class input {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int number;

        do {
            System.out.println("Please enter a positive number!");
            while (!sc.hasNextInt()) //scan
            {
                System.out.println("That's not a number!");
                sc.nextLine();  //scan
            }
            number = sc.nextInt();
            sc.nextLine();  //scan

        }
        while (number <= 0);

        System.out.println("Thank you! The positive number is " + number);
    }
}

Here is the result:
Please enter a positive number!
dfd
That's not a number!
-2323
Please enter a positive number!
dfd dfd
That's not a number!
fdfd 322
That's not a number!
23 sdfd
Thank you! The positive number is 23

this time the "That's not a number" printed only once, but I really can't see why by putting "sc.nextLine()" helps solves the problem.....

and still, I come another question, what if I also want to validate the user input when they input something like "23 sdfd","23 2323" and the problem still provide that the information to prompt the user to try again until they input correct int?


回答1:


It's because number = sc.nextInt(); did not consume the newline character after you entered -2232. So it evaluated that newline character, determined that it's not an int, and prompted "That's not a number!". Then you entered dfd dfd, it determined that that was not an int, and prompted "That's not a number!" again.

Your edit fixed the problem by consuming the newline character after it read in the int.

see Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods



来源:https://stackoverflow.com/questions/23021130/using-scanner-to-validate-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!