English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java Scanner and its methods through examples.
The PrintWriter class in the java.io package can be used to write output data in a normally readable form (text).
The Scanner class in the java.util package is used to read input data from different sources (such as input streams, users, files, etc.). Let's take an example.
import java.util.Scanner; class Main { public static void main(String[] args) { //Create an object Scanner Scanner input = new Scanner(System.in); System.out.print("Enter name: "); // Receive input from the keyboard String name = input.nextLine(); // Print output name System.out.println("My name is " + name); // Closes the scanner input.close(); } }
Output Result
Enter name: Jack My name is Jack
Please note the following line in the above example
Scanner input = new Scanner(System.in);
Here, we create a Scanner object named input.
The System.in parameter is used to obtain input from the standard input. It's like getting input from the keyboard.
Then, we use the nextLine() method of the Scanner class to read a line of text from the user.
Now that you have some understanding of Scanner, let's explore it further.
From the above example, you can see that we need to import the java.util.Scanner package first before we can use the Scanner class.
import java.util.Scanner;
As mentioned above, after importing the package, you can create a Scanner object.
//Read input from an input stream Scanner sc1 = new Scanner(InputStream input); //Read input from a file Scanner sc2 = new Scanner(File file); //Read input from a string Scanner sc3 = new Scanner(String str);
Here, we create objects of the Scanner class, which will read inputs fromInputStream,file and string input reading.
The Scanner class provides various methods that allow us to read different types of input.
Method | Description |
---|---|
nextInt() | Read an int value from the user |
nextFloat() | Read a float value from the user |
nextBoolean() | Read a boolean value from the user |
nextLine() | Read a line of text from the user |
next() | Read a word from the user |
nextByte() | Read a byte value from the user |
nextDouble() | Read a double value from the user |
nextShort() | Read a short value from the user |
nextLong() | Read a long value from the user |
import java.util.Scanner; class Main { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); System.out.println("Input an integer: ") //Read an int value int data1 = input.nextInt(); System.out.println("Using nextInt(): ") + data1); input.close(); } }
Output Result
Input an integer: 22 Using nextInt(): 22
In the above example, we used nextInt() method to read integer values.
import java.util.Scanner; class Main { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); System.out.print("Input a double precision value: ") //Read a double precision value double value = input.nextDouble(); System.out.println("Using nextDouble(): ") + value); input.close(); } }
Output Result
Input a double precision value: 33.33 Using nextDouble(): 33.33
In the above example, we used nextDouble() method to read floating-point values.
import java.util.Scanner; class Main { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); //Read the entire word String value = input.next(); System.out.println("Using next(): ") + value); input.close(); } }
Output Result
Enter your name: Jonny Walker Using next(): Jonny
In the above example, we used the next() method to read a string from the user.
Here, we provide the full name. However, the next() method only reads the name.
This is because the next() method reads the input up tospacescharacters. Once a space is encountered, it returns the string (excluding spaces).
import java.util.Scanner; class Main { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); //Read a full line String value = input.nextLine(); System.out.println("Using nextLine(): "); + value); input.close(); } }
Output Result
Enter your name: Jonny Walker Using nextLine(): Jonny Walker
In the first example, we used the nextLine() method to read a string from the user.
Unlike next(), the nextLine() method reads the entire input line including spaces. It terminates when it encounters the next line character \n.
Java scanner can also be used to read large integers and large decimals.
nextBigInteger() - Read large integer values from the user
nextBigDecimal() - Read large decimal values from the user
import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; class Main { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); System.out.print("Enter a large integer: "); //Read a large integer BigInteger value1 = input.nextBigInteger(); System.out.println("Using nextBigInteger(): "); + value1); System.out.print("Enter a large decimal: "); //Read a large decimal BigDecimal value2 = input.nextBigDecimal(); System.out.println("Using nextBigDecimal(): ") + value2); input.close(); } }
Output Result
Enter a large integer: 987654321 Using nextBigInteger(): 987654321 Enter a large decimal: 9.55555 Using nextBigDecimal(): 9.55555
In the above example, we use the java.math.BigInteger and java.math.BigDecimal packages to read BigInteger and BigDecimal, respectively.
The scanner class reads the entire line and splits the line into tokens. Tokens are small elements that have a certain 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 split the string into tokens: “ He ”,“ is 22 ”. Then, the object traverses each token and uses its different methods to read each token.
NoteBy default, spaces are used to delimit tokens.