Scanner is one of the built-in Java classes that is used to interact with the users. It belongs to the “java.util” package. Using this class, we can get the input of string-type or primitive types like “int”, “float”, etc. The Scanner class is the simplest way of getting user input; however, it’s not the best choice where time is a constraint.
Let’s learn how to import and use the Scanner class in Java using suitable examples.
Java Scanner Class
The Java Scanner Class lets us read data of various types and from different sources like strings, files, streams, etc. For this purpose, it offers different constructors and methods, which will be explained in the upcoming sections.
1) Scanner Class Constructors
The Scanner class supports several constructors, which are shown in the following table along with the description:
| Constructor | Description |
| Scanner(File sourceFile); | It creates a new Scanner object that reads input from the specified file. |
| Scanner(File sourceFile, String charsetName); | It creates a new Scanner object that reads/scans input from the selected file using the provided character encoding. |
| Scanner(InputStream source); | It is used to read input from the given InputStream. |
| Scanner(InputStream source, String charsetName); | It creates a new Scanner object that reads input from the specified InputStream using the provided character encoding. |
| Scanner(Readable src); | It constructs a new scanner object that reads input from the specified readable source. |
| Scanner(String src); | It creates a new scanner object that scans/reads the given string/text. |
| Scanner(ReadableByteChannel src); | It constructs a new scanner object that scans input from the specified ReadableByteChannel source. |
| Scanner(ReadableByteChannel src, String charsetName); | Creates a new scanner object that scans input from the specified ReadableByteChannel source and decodes the bytes via the specified charset. |
| Scanner(Path src); | It constructs a new Scanner object that scans input from the specified file, indicated by the Path object. |
| Scanner(Path src, String charsetName); | It creates a new Scanner object that reads input from the file or path specified by the Path object and uses the specified character set to decode the bytes from the input stream into characters. |
2) Scanner Class Methods
The Java Scanner class offers numerous built-in methods to execute different tasks. The functionality of each method is illustrated in the following table:
| Query | Answer |
| How to Close a Scanner in Java? | Use the close() method. |
| How to Reset a Scanner in Java? | Use the reset() method. |
| How to Get String Representation of a Scanner in Java? | Invoke the toString() method. |
| How to Read a Boolean Value in Java? | Use the nextBoolean() method. |
| How to Read a Double Value in Java? | Use the nextDouble() method. |
| How to Read a Float Value in Java? | Invoke the nextFloat() method. |
| How to Read an Integer Value in Java? | Use the nextInt() method. |
| How to Read a Byte Value in Java? | Use the nextByte() method. |
| How to Read a Short Value in Java? | Use the nextShort() method. |
| How to Read a Long Value in Java? | Use the nextLong() method. |
| How to Read a Line in Java? | Use the nextLine() method. |
| How to Set the Scanner’s Default Radix? | Use the useRadix() method. |
| How to Check/Find the Next Occurrence of a Specific Pattern/Input in Java? | Use the findInLine() method. |
| How to Check if There is Another Line in This Scanner’s Input? | Use the hasNextLine() method. |
| How to Check if the Next Token in a Java Scanner Object’s Input can be interpreted as a Specific Data Type? | Use the hasNextXYZ() method. Replace the XYZ with the desired data type, like int, float, long, etc. For example, the hasNextLong() method checks if the next token can be interpreted/read as a Long data type. |
| How to Check if there is Another Token in the Scanner? | Use the hasNext() method. |
| How to Get a Complete Next Token from this Scanner? | Use the next() method. |
| How to Scan/Read the Next Token of the Input as a Specific Type like int, boolean, byte, float, etc. | Use the nextXYZ() method. Replace XYZ with the desired data type. For instance, the nextByte() reads the subsequent input token as a byte. |
| How to Skip an Input that Matches the Given Pattern? | Use the skip() method. |
| How to Set this Scanner’s Delimiting Pattern in Java? | Use the useDelimiter() method to specify this Scanner’s delimiting pattern. |
| How to Get the Current Delimiting Pattern of this Scanner? | Invoke the delimiter() method to retrieve the current delimiting pattern. |
| How to Get the Matching Result of the Last Scanning Operation? | Use the match() method. |
What is a System.in in Java
The “System.in” is an input stream object that is used to get the user’s input via the keyboard.
How to Import Scanner Class in Java
To use the Scanner class in Java, first, we need to import it into our program. For this purpose, copy and paste the below line of code at the start of your program:
import java.util.Scanner;
Note: To learn more methods of importing the Scanner class in Java, read our dedicated guide titled “How to Import Scanner in Java”.
How to Get User Input Using Java Scanner Class
Java offers different methods to get the user’s input. Each method accepts a specific type of input from the user, as discussed in the above table. In the example below, we’ll show you how to interpret various types of data from the user:
import java.util.Scanner;
public class ScannerExamples {
public static void main(String[] args) {
Scanner scanInput = new Scanner(System.in);
System.out.println("Please Enter Your Name: ");
String userName = scanInput.nextLine();
System.out.println("Enter Your ID: ");
Integer userID = scanInput.nextInt();
System.out.println("Enter Your Expected Salary: ");
Double userSal = scanInput.nextDouble();
System.out.println("Are You Above 25: ");
Boolean isEligible = scanInput.nextBoolean();
System.out.println("Your Name is: " + userName);
System.out.println("Your ID: " + userID);
System.out.println("Your Salary: " + userSal);
System.out.println("Are You Above 25: " + isEligible);
scanInput.close();
}
}
Code Explanation:
- First, we import the Scanner class to use its methods in our code.
- We create an object of the Java Scanner class named “scanInput”. After this, we pass the input stream to the Scanner() constructor to get the user’s input.
- Next, we use the “nextLine()”, “nextInt()”, “nextDouble()”, and “nextBoolean()” methods to read a string, an integer, a double, and a boolean value from the user, respectively.
- Finally, we print each user-entered value using the println() method.
Output:

How to Check if a Java Scanner Has a Token
In the below code, we have two strings, inputString1, and inputString2. We create a couple of scanner objects and pass the given strings to the respective Scanner constructors, as shown below:
import java.util.Scanner;
public class ScannerExamples {
public static void main(String[] args) {
String inputString1 = "Hello! Let's Learn Java Programming";
String inputString2 = "";
Scanner scanInput1 = new Scanner(inputString1);
Scanner scanInput2 = new Scanner(inputString2);
System.out.println("Scanner1 Has a String Token? " + scanInput1.hasNext());
System.out.println("Scanner1 Has an Integer Token? " + scanInput1.hasNextInt());
System.out.println("Scanner1 Has a Boolean Token? " + scanInput1.hasNextBoolean());
System.out.println("Scanner2 Has a Token? " + scanInput2.hasNext());
scanInput1.close();
scanInput2.close();
}
}
Code Explanation
- We use the hasNext() method to check if the scanner “scanInput1” has a string token and prints its resultant value on the console.
- Also, we invoke the hasNextInt() and hasNextBoolean() methods on the “scanInput” scanner to check if it has an integer and a boolean token.
- Finally, we use the hasNext() method on the “scanInput2” scanner to check if it has a string token.
Output:

Note: By default, the delimiter of this scanner is whitespace; however, we can set it according to our preferences using the useDelimiter() method.
How to Parse an Input in Java Using Scanner Class
We can parse an input in Java using the Scanner class’s useDelimiter() method. Here’s a simple example:
import java.util.Scanner;
public class ScannerExamples {
public static void main(String[] args) {
String inputString1 = "Hello-Let's-Learn-Java-Programming";
Scanner scanInput1 = new Scanner(inputString1);
scanInput1.useDelimiter("-");
System.out.println("The Parsed Input is ==> ");
while (scanInput1.hasNext()){
System.out.println(scanInput1.next());
}
scanInput1.close();
}
}
Code Explanation:
- We create a string “Hello-Let’s-Learn-Java-Programming” and initialize it to the “inputString1” variable.
- We invoke the Scanner() constructor to create a scanner object and pass the input string to it.
- Next, we use the “useDelimiter()” method to specify the delimiting pattern (the input string will be parsed accordingly).
- After this, we use the while loop that keeps iterating until the scanner has a token.
- Within the while loop, we use the next() method within the println() method to read a word from the input string and print it on the console.
This way the entire string will be parsed, and the final output will be something like this:

That’s all about the Java Scanner Class.
Final Thoughts
Scanner is one of the most widely used built-in Java classes that lets us get the user input of primitive types, like int, float, double, etc., and strings. We can import this class from the “java.util” package and then create its object using the Scanner constructor. We can use this object to invoke any of its methods to perform a specific task.