In this post, we will see how to do input validation in java using Scanner class.
The Java Scanner
class is used to get input from user. It provides several methods to get input of different types and to validate the input as well. Here we will see some of these methods that can be used to get user input and do the input validation in java.
Table of Contents
Scanner Methods to Validate User Input
Method | Description |
---|---|
boolean hasNext​(String pattern) | It returns true if the next token matches the pattern constructed from the specified string. |
boolean hasNext​(Pattern pattern) | It returns true if the next complete token matches the specified pattern. |
boolean hasNextBigDecimal​() | It returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method. |
boolean hasNextBigInteger​() | It returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the nextBigInteger() method. |
boolean hasNextBigInteger​(int radix) | It returns true if the next token in this scanner's input can be interpreted as a BigInteger in the specified radix using the nextBigInteger() method. |
boolean hasNextBoolean​() | It returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false". |
boolean hasNextByte​() | It returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method. |
boolean hasNextByte​(int radix) | It returns true if the next token in this scanner's input can be interpreted as a byte value in the specified radix using the nextByte() method. |
boolean hasNextDouble​() | It returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. |
boolean hasNextFloat​() | It returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method. |
boolean hasNextInt​() | It returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. |
boolean hasNextInt​(int radix) | It returns true if the next token in this scanner's input can be interpreted as an int value in the specified radix using the nextInt() method. |
boolean hasNextLine​() | It returns true if there is another line in the input of this scanner. |
boolean hasNextLong​() | It returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method. |
boolean hasNextLong​(int radix) | It returns true if the next token in this scanner's input can be interpreted as a long value in the specified radix using the nextLong() method. |
boolean hasNextShort​() | It returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method. |
boolean hasNextShort​(int radix) | It returns true if the next token in this scanner's input can be interpreted as a short value in the specified radix using the nextShort() method. |
Scanner Methods to get User Input
Method | Description |
---|---|
String next​(String pattern) | It returns the next token if it matches the pattern constructed from the specified string. |
String next​(Pattern pattern) | It returns the next token if it matches the specified pattern. |
BigDecimal nextBigDecimal​() | It scans the next token of the input as a BigDecimal. |
BigInteger nextBigInteger​() | It scans the next token of the input as a BigInteger. |
BigInteger nextBigInteger​(int radix) | It scans the next token of the input as a BigInteger. |
boolean nextBoolean​() | It scans the next token of the input into a boolean value and returns that value. |
byte nextByte​() | It scans the next token of the input as a byte. |
byte nextByte​(int radix) | It scans the next token of the input as a byte. |
double nextDouble​() | It scans the next token of the input as a double. |
float nextFloat​() | It scans the next token of the input as a float. |
int nextInt​() | It scans the next token of the input as an int. |
int nextInt​(int radix) | It scans the next token of the input as an int. |
String nextLine​() | it advances this scanner past the current line and returns the input that was skipped. |
long nextLong​() | It scans the next token of the input as a long. |
long nextLong​(int radix) | It scans the next token of the input as a long. |
short nextShort​() | It scans the next token of the input as a short. |
short nextShort​(int radix) | It scans the next token of the input as a short. |
Input validation using Scanner class
We can do different type of input validation using different hasNextXXX()
methods.
Validate integer input using Scanner in Java
We can use hasNextInt()
method to check whether the input is integer and then get that using nextInt()
method. See the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; public class Main { public static void main(String args[]) { int input = 0; System.out.println("Enter an Integer value "); Scanner sc = new Scanner(System.in); if(sc.hasNextInt()) { input = sc.nextInt(); if(input>0) System.out.println("You entered a positive integer "+input); else { System.out.println("You entered a negative integer "+input); } }else { System.out.println("Please Enter Valid Integer"); } } } |
Output
50
You entered a positive integer 50
Validate Floating point input using Scanner in Java
To validate floating-point values, we used hasNextDouble()
method that returns true if the input is floating type and nextDouble() method is used to get the user input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; public class Main { static Double input; public static void main(String args[]) { System.out.println("Enter a floating point value "); Scanner sc = new Scanner(System.in); if(sc.hasNextDouble()) { input = sc.nextDouble(); if(input>0) System.out.println("You entered a positive value "+input); else { System.out.println("You entered a negative value "+input); } }else { System.out.println("Please Enter a Valid Value"); } } } |
Output:
25.21
You entered a positive value 25.21
Validate Boolean input using Scanner in Java
We can use hasNextBoolean()
method to check whether the input is a valid boolean or not and nextBoolean()
method is used to get the input value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; public class Main { static Boolean input; public static void main(String args[]) { System.out.println("Enter a boolean value "); Scanner sc = new Scanner(System.in); if(sc.hasNextBoolean()) { input = sc.nextBoolean(); System.out.println("You entered a boolean value "+input); }else { System.out.println("Please Enter a Valid Value"); } } } |
Output
false
You entered a boolean value false
Validate String input using Scanner in Java
To validate a string value, we can use regex
to get string in a specific format. Here, the hasNext()
takes a regex to validate a string that can contain only alphabets. See the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; public class Main { static String input; public static void main(String args[]) { System.out.println("Enter a String "); Scanner sc = new Scanner(System.in); if(sc.hasNext("[A-Za-z]*")) { input = sc.next(); System.out.println("You entered a string value "+input); }else { System.out.println("Please Enter a Valid Value"); } } } |
Output
java
You entered a string value java
That’s all about Input validation in java using Scanner.