In this tutorial, we will learn about the Java Scanner and its strategies with the help of examples.
The Scanner class of the java. util package is used to read input data from various sources like input streams, users, files, and so on. let’s take an example.
In this article, you will learn-
- 1 Example 1: Read a Line of Text Using Scanner
- 2 Import Scanner Class
- 3 Make a Scanner Object in Java
- 4 Java Scanner Methods to Take Input
- 5 Example 2: Java Scanner nextInt()
- 6 Example 3: Java Scanner nextDouble()
- 7 Example 4: Java Scanner next()
- 8 Example 5: Java Scanner nextLine()
- 9 Java Scanner with BigInteger and BigDecimal
- 10 Example 6: Read BigInteger and BigDecimal
- 11 Working of Java Scanner
Example 1: Read a Line of Text Using Scanner
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// takes input from the keyboard
String name = input.nextLine();
// prints the name
System.out.println("My name is " + name);
// closes the scanner
input.close();
}
}
Output
Enter your name: salman
My name is salman
In the above example, notice the line
Scanner input = new Scanner(System.in);
Here, we have made an object of Scanner named input.
The System.in parameter is used to take input from the standard input. It works much the same as taking inputs from the keyboard.
We have then used the nextLine() strategy for the Scanner class to read a line of text from the user.
Since you have some idea about Scanner, let’s explore more about it.
Import Scanner Class
As should be obvious from the above example, we need to import the java.util.Scanner package before we can use the Scanner class.
import java.util.Scanner;
Make a Scanner Object in Java
When we import the package, here is the means by which we can make 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 made objects of the Scanner class that will read input from InputStream, File, and String individually.
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()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates a Scanner object
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
// reads an int value
int data1 = input.nextInt();
System.out.println("Using nextInt(): " + data1);
input.close();
}
}
Output
Enter an integer:
22
Using nextInt(): 22
In the above example, we have used the nextInt() technique to read a integer value.
Example 3: Java Scanner nextDouble()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter Double value: ");
// reads the double value
double value = input.nextDouble();
System.out.println("Using nextDouble(): " + value);
input.close();
}
}
Output
Enter Double value: 33.33
Using nextDouble(): 33.33
In the above example, we have used the nextDouble() technique to read a floating point value.
Example 4: Java Scanner next()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// reads the entire word
String value = input.next();
System.out.println("Using next(): " + value);
input.close();
}
}
Output
Enter your name: Salman Khan
Using next(): Salman
In the above example, we have used the next() technique to read a string from the user.
Here, we have given the complete name. However, the next() technique just reads the first name.
This is on the grounds that the next() strategy reads input up to the whitespace character. When the whitespace is experienced, it returns the string (excluding the whitespace).
Example 5: Java Scanner nextLine()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// reads the entire line
String value = input.nextLine();
System.out.println("Using nextLine(): " + value);
input.close();
}
}
Output
Enter your name: Salman Khan
Using nextLine(): Salman Khan
In the first example, we have used the nextLine() strategy to read a string from the user.
Not at all like straightaway(), the nextLine() technique reads the whole line of input including spaces. The strategy is ended 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 6: Read BigInteger and BigDecimal
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter a big integer: ");
// reads the big integer
BigInteger value1 = input.nextBigInteger();
System.out.println("Using nextBigInteger(): " + value1);
System.out.print("Enter a big decimal: ");
// reads the big decimal
BigDecimal value2 = input.nextBigDecimal();
System.out.println("Using nextBigDecimal(): " + value2);
input.close();
}
}
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 individually.
Working of Java Scanner
The Scanner class reads a whole line and partitions the line into tokens. Tokens are little components that make them intend to the Java compiler. For instance,
Assume there is an input string:
He is 22
For this situation, the scanner object will read the whole line and partitions the string into tokens: “He“, “is” and “22“. The object at that point repeats over every token and reads each token using its various strategies.
Note: By default, whitespace is used to divide tokens.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.