Taking User Input in Java Using Scanner
Introduction#
Until now, we have stored values directly inside our Java programs.
For example:
int age = 20;
String name = "Ravi";
But in real applications, we often need to get values from the user while the program is running.
For example:
Enter your name: Ravi
Enter your age: 20
Java provides the Scanner class to take input from the user.
What Is Scanner?#
Scanner is a Java class used to read input from the user.
It belongs to the java.util package.
To use Scanner, we first import it:
import java.util.Scanner;
Then we create a Scanner object:
Scanner sc = new Scanner(System.in);
Here:
Scanneris the class.scis the reference variable.new Scanner(System.in)creates the Scanner object.System.inrepresents input from the keyboard.
Basic Scanner Program#
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Your age is: " + age);
}
}
Output
Enter your age: 20
Your age is: 20
The statement:
int age = sc.nextInt();
reads an integer entered by the user and stores it in age.
Scanner Methods#
Different data types use different Scanner methods.
| Data Type | Scanner Method |
|---|---|
byte |
nextByte() |
short |
nextShort() |
int |
nextInt() |
long |
nextLong() |
float |
nextFloat() |
double |
nextDouble() |
boolean |
nextBoolean() |
String |
next() |
String with spaces |
nextLine() |
Taking String Input#
Scanner provides two commonly used methods for reading strings:
next()
and
nextLine()
Using next()#
next() reads only one word.
String name = sc.next();
If the user enters:
Ravi Kumar
only:
Ravi
will be read.
Using nextLine()#
nextLine() reads the complete line.
String name = sc.nextLine();
If the user enters:
Ravi Kumar
the complete value:
Ravi Kumar
will be read.
Simple Difference#
next() → reads one word
nextLine() → reads the complete line
Taking Character Input
Scanner does not have a nextChar() method.
To read a character, we can use:
char grade = sc.next().charAt(0);
Example:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter grade: ");
char grade = sc.next().charAt(0);
System.out.println("Grade = " + grade);
}
}
If the user enters:
A
the value stored in grade is:
A
Taking Multiple Inputs
We can use the same Scanner object to take multiple inputs.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter percentage: ");
double percentage = sc.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Percentage: " + percentage);
}
}
Example:
Enter name: Ravi Kumar
Enter age: 20
Enter percentage: 85.5
Name: Ravi Kumar
Age: 20
Percentage: 85.5
Check Your Understanding
1. What will be the output?#
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
If the user enters:
10
20
2. What is the difference between these two?#
String name = sc.next();
and
String name = sc.nextLine();
3. Which Scanner method would you use for each?#
- Student age
- Product price
- Student name with spaces
- Student grade
- Passed or failed
4. Find the problem#
int age = sc.nextInt();
String name = sc.nextLine();
Why can this cause a problem when reading the name, and how can you fix it?