The Scanner class of the java.util package is used to read input data from different sources like input streams, users, files, etc. Let's take an example.
Example 1: Read a Line of Text Using Scanner
Output
Enter your name: Kelvin My name is Kelvin
In the above example, notice the line
Scanner input = new Scanner(System.in);
Here, we have created an object of Scanner named input.
The System.in parameter is used to take input from the standard input. It works just like taking inputs from the keyboard.
We have then used the nextLine() method of the Scanner class to read a line of text from the user.
Now that you have some idea about Scanner, let's explore more about it.
Import Scanner Class
As we can see from the above example, we need to import the java.util.Scanner package before we can use the Scanner class.
import java.util.Scanner;
To learn more about importing packages, visit Java Packages.
Create a Scanner Object in Java
Once we import the package, here is how we can create Scanner objects.
// read input from the input stream
Scanner sc1 = new Scanner(InputStream input);
// read input from files
Scanner sc2 = new Scanner(File file);
// read input from a string
Scanner sc3 = new Scanner(String str);
Here, we have created objects of the Scanner class that will read input from InputStream, File, and String respectively.
Java Scanner Methods to Take Input
The Scanner class provides various methods that allow us to read inputs of different types.
| Method | Description |
|---|---|
nextInt() |
reads an int value from the user |
nextFloat() |
reads a float value form the user |
nextBoolean() |
reads a boolean value from the user |
nextLine() |
reads a line of text from the user |
next() |
reads a word from the user |
nextByte() |
reads a byte value from the user |
nextDouble() |
reads a double value from the user |
nextShort() |
reads a short value from the user |
nextLong() |
reads a long value from the user |
Example 2: Java Scanner nextInt()
Output
Enter an integer: 22 Using nextInt(): 22
In the above example, we have used the nextInt() method to read an integer value.
Example 3: Java Scanner nextDouble()
Output
Enter Double value: 33.33 Using nextDouble(): 33.33
In the above example, we have used the nextDouble() method to read a floating-point value.
Example 4: Java Scanner next()
Output
Enter your name: Jonny Walker Using next(): Jonny
In the above example, we have used the next() method to read a string from the user.
Here, we have provided the full name. However, the next() method only reads the first name.
This is because the next() method reads input up to the whitespace character. Once the whitespace is encountered, it returns the string (excluding the whitespace).
Example 5: Java Scanner nextLine()
Output
Enter your name: Jonny Walker Using nextLine(): Jonny Walker
In the first example, we have used the nextLine() method to read a string from the user.
Unlike next(), the nextLine() method reads the entire line of input including spaces. The method is terminated when it encounters a next line character, \n.
Recommended Reading: Java Scanner skipping the nextLine().
Java Scanner with BigInteger and BigDecimal
Java scanner can also be used to read the big integer and big decimal numbers.
- nextBigInteger() - reads the big integer value from the user
- nextBigDecimal() - reads the big decimal value from the user
Example 4: Read BigInteger and BigDecimal
Output
Enter a big integer: 987654321 Using nextBigInteger(): 987654321 Enter a big decimal: 9.55555 Using nextBigDecimal(): 9.55555
In the above example, we have used the java.math.BigInteger and java.math.BigDecimal package to read BigInteger and BigDecimal respectively.
Working of Java Scanner
The Scanner class reads an entire line and divides the line into tokens. Tokens are small elements that have some meaning to the Java compiler. For example,
Suppose there is an input string:
He is 22
In this case, the scanner object will read the entire line and divides the string into tokens: "He", "is" and "22". The object then iterates over each token and reads each token using its different methods.
Note: By default, whitespace is used to divide tokens.