validation – Validating input using java.util.Scanner

validation – Validating input using java.util.Scanner

Overview of Scanner.hasNextXXX methods

java.util.Scanner has many hasNextXXX methods that can be used to validate input. Heres a brief overview of all of them:

Scanner is capable of more, enabled by the fact that its regex-based. One important feature is useDelimiter(String pattern), which lets you define what pattern separates your tokens. There are also find and skip methods that ignores delimiters.

The following discussion will keep the regex as simple as possible, so the focus remains on Scanner.


Example 1: Validating positive ints

Heres a simple example of using hasNextInt() to validate positive int from the input.

Scanner sc = new Scanner(System.in);
int number;
do {
    System.out.println(Please enter a positive number!);
    while (!sc.hasNextInt()) {
        System.out.println(Thats not a number!);
        sc.next(); // this is important!
    }
    number = sc.nextInt();
} while (number <= 0);
System.out.println(Thank you! Got  + number);

Heres an example session:

Please enter a positive number!
five
Thats not a number!
-3
Please enter a positive number!
5
Thank you! Got 5

Note how much easier Scanner.hasNextInt() is to use compared to the more verbose try/catch Integer.parseInt/NumberFormatException combo. By contract, a Scanner guarantees that if it hasNextInt(), then nextInt() will peacefully give you that int, and will not throw any NumberFormatException/InputMismatchException/NoSuchElementException.

Related questions


Example 2: Multiple hasNextXXX on the same token

Note that the snippet above contains a sc.next() statement to advance the Scanner until it hasNextInt(). Its important to realize that none of the hasNextXXX methods advance the Scanner past any input! You will find that if you omit this line from the snippet, then itd go into an infinite loop on an invalid input!

This has two consequences:

  • If you need to skip the garbage input that fails your hasNextXXX test, then you need to advance the Scanner one way or another (e.g. next(), nextLine(), skip, etc).
  • If one hasNextXXX test fails, you can still test if it perhaps hasNextYYY!

Heres an example of performing multiple hasNextXXX tests.

Scanner sc = new Scanner(System.in);
while (!sc.hasNext(exit)) {
    System.out.println(
        sc.hasNextInt() ? (int)  + sc.nextInt() :
        sc.hasNextLong() ? (long)  + sc.nextLong() :  
        sc.hasNextDouble() ? (double)  + sc.nextDouble() :
        sc.hasNextBoolean() ? (boolean)  + sc.nextBoolean() :
        (String)  + sc.next()
    );
}

Heres an example session:

5
(int) 5
false
(boolean) false
blah
(String) blah
1.1
(double) 1.1
100000000000
(long) 100000000000
exit

Note that the order of the tests matters. If a Scanner hasNextInt(), then it also hasNextLong(), but its not necessarily true the other way around. More often than not youd want to do the more specific test before the more general test.


Example 3 : Validating vowels

Scanner has many advanced features supported by regular expressions. Heres an example of using it to validate vowels.

Scanner sc = new Scanner(System.in);
System.out.println(Please enter a vowel, lowercase!);
while (!sc.hasNext([aeiou])) {
    System.out.println(Thats not a vowel!);
    sc.next();
}
String vowel = sc.next();
System.out.println(Thank you! Got  + vowel);

Heres an example session:

Please enter a vowel, lowercase!
5
Thats not a vowel!
z
Thats not a vowel!
e
Thank you! Got e

In regex, as a Java string literal, the pattern [aeiou] is what is called a character class; it matches any of the letters a, e, i, o, u. Note that its trivial to make the above test case-insensitive: just provide such regex pattern to the Scanner.

API links

Related questions

References


Example 4: Using two Scanner at once

Sometimes you need to scan line-by-line, with multiple tokens on a line. The easiest way to accomplish this is to use two Scanner, where the second Scanner takes the nextLine() from the first Scanner as input. Heres an example:

Scanner sc = new Scanner(System.in);
System.out.println(Give me a bunch of numbers in a line (or exit));
while (!sc.hasNext(exit)) {
    Scanner lineSc = new Scanner(sc.nextLine());
    int sum = 0;
    while (lineSc.hasNextInt()) {
        sum += lineSc.nextInt();
    }
    System.out.println(Sum is  + sum);
}

Heres an example session:

Give me a bunch of numbers in a line (or exit)
3 4 5
Sum is 12
10 100 a million dollar
Sum is 110
wait what?
Sum is 0
exit

In addition to Scanner(String) constructor, theres also Scanner(java.io.File) among others.


Summary

  • Scanner provides a rich set of features, such as hasNextXXX methods for validation.
  • Proper usage of hasNextXXX/nextXXX in combination means that a Scanner will NEVER throw an InputMismatchException/NoSuchElementException.
  • Always remember that hasNextXXX does not advance the Scanner past any input.
  • Dont be shy to create multiple Scanner if necessary. Two simple Scanner is often better than one overly complex Scanner.
  • Finally, even if you dont have any plans to use the advanced regex features, do keep in mind which methods are regex-based and which arent. Any Scanner method that takes a String pattern argument is regex-based.
    • Tip: an easy way to turn any String into a literal pattern is to Pattern.quote it.

Heres a minimalist way to do it.

System.out.print(Please enter an integer: );
while(!scan.hasNextInt()) scan.next();
int demoInt = scan.nextInt();

validation – Validating input using java.util.Scanner

For checking Strings for letters you can use regular expressions for example:

someString.matches([A-F]);

For checking numbers and stopping the program crashing, I have a quite simple class you can find below where you can define the range of values you want.
Here

    public int readInt(String prompt, int min, int max)
    {
    Scanner scan = new Scanner(System.in);

    int number = 0;

    //Run once and loop until the input is within the specified range.
    do 
    {
        //Print users message.
        System.out.printf(n%s > , prompt);

        //Prevent string input crashing the program.
        while (!scan.hasNextInt()) 
        {
            System.out.printf(Input doesnt match specifications. Try again.);
            System.out.printf(n%s > , prompt);
            scan.next(); 
        }

        //Set the number.
        number = scan.nextInt();

        //If the number is outside range print an error message.
        if (number < min || number > max)
            System.out.printf(Input doesnt match specifications. Try again.);

    } while (number < min || number > max);

    return number;
}

Leave a Reply

Your email address will not be published.