简体   繁体   中英

Using Scanner to validate user input

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?

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

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