Scanner class is exists in java.util package, which is responsible to read the data from multiple sources like input streams, users, files etc. Let's take an example.
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();
}
}
In the above example, notice the line
Scanner input = new Scanner(System.in);
Here, we have created an object of Scanner named input.
If we want to read the data from the standard input then System.in parameter will be used. 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.
Import Scanner
To use the methods which are available in scanner class, first we must import the package like below.
import java.util.Scanner;
Create a Scanner Object in Java
Once we import the package, next 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);
See above, we have created three different scanner class objects to read the input from different sources InputStream, File and String.
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();
}
}
In the above example, we have used the nextInt() method to read an 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();
}
}
In the above example, we have used the nextDouble() method 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();
}
}
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.
next() method will only read the given input up to the whitespace character. While reading if its found any whitespace character then it will return the string.
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();
}
}
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. nextLine() method will read the given input up to the new line character which is \n.
Scanner example with BigInteger and BigDecimal
Example 4: 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();
}
}
In the above example, we have used the java.math.BigInteger and java.math.BigDecimal package to read BigInteger and BigDecimal respectively.