Lesson 2.6: Taking User Input Using Scanner
Introduction
So far, all the programs we have written used hardcoded values. This means the values were already written inside the program.
However, real-world applications allow users to enter their own data. For example:
-
A calculator asks users to enter two numbers.
-
An ATM asks users to enter their PIN.
-
A student management system asks users to enter their name, roll number, and marks.
To make Java programs interactive, Java provides the Scanner class, which allows us to take input from the keyboard.
In this lesson, you will learn how to use the Scanner class to accept different types of user input.
Learning Objectives
After completing this lesson, you will be able to:
-
Understand the purpose of the Scanner class.
-
Import the Scanner class.
-
Create a Scanner object.
-
Take different types of user input.
-
Read integers, decimals, characters, and strings.
-
Build simple interactive Java programs.
What is the Scanner Class?
The Scanner class is a predefined class in the java.util package.
It is used to read input from various sources such as:
-
Keyboard
-
Files
-
Strings
In this course, we will use the Scanner class to take input from the keyboard.
Importing the Scanner Class
Before using Scanner, import it using:
import java.util.Scanner;
This statement tells Java that we want to use the Scanner class.
Creating a Scanner Object
After importing Scanner, create an object:
Scanner sc = new Scanner(System.in);
Explanation
-
Scanner→ Class name -
sc→ Object name -
new→ Creates a new object -
System.in→ Reads input from the keyboard
Taking Integer Input
Example:
import java.util.Scanner; public class InputExample { 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("Age = " + age); } }
Sample Output
Enter your age: 20
Age = 20
Taking Floating-Point Input
Using float
float percentage = sc.nextFloat();
Using double
double salary = sc.nextDouble();
Taking String Input
Using next()
next() reads only one word.
Example:
String name = sc.next(); System.out.println(name);
Input:
Rahul
Output:
Rahul
If the user enters:
Rahul Kumar
Output:
Rahul
Only the first word is read.
Using nextLine()
nextLine() reads the entire line, including spaces.
Example:
String fullName = sc.nextLine(); System.out.println(fullName);
Input:
Rahul Kumar
Output:
Rahul Kumar
Taking Character Input
Scanner does not provide a direct method for reading a character.
Instead, use:
char grade = sc.next().charAt(0);
Example:
System.out.print("Enter Grade: "); char grade = sc.next().charAt(0); System.out.println("Grade = " + grade);
Complete Example
import java.util.Scanner; public class StudentDetails { 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.print("Enter Grade: "); char grade = sc.next().charAt(0); System.out.println("\n------ Student Details ------"); System.out.println("Name : " + name); System.out.println("Age : " + age); System.out.println("Percentage : " + percentage); System.out.println("Grade : " + grade); sc.close(); } }
Sample Output
Enter Name: Rahul Kumar
Enter Age: 20
Enter Percentage: 89.5
Enter Grade: A
------ Student Details ------
Name : Rahul Kumar
Age : 20
Percentage : 89.5
Grade : A
Scanner Methods
| Method | Description |
|---|---|
nextInt() |
Reads an integer |
nextFloat() |
Reads a float value |
nextDouble() |
Reads a double value |
nextLong() |
Reads a long value |
nextBoolean() |
Reads a boolean value |
next() |
Reads a single word |
nextLine() |
Reads an entire line |
next().charAt(0) |
Reads a single character |
Difference Between next() and nextLine()
next() |
nextLine() |
|---|---|
| Reads one word | Reads the complete line |
| Stops at space | Includes spaces |
| Suitable for single-word input | Suitable for full names or addresses |
Common Mistakes
Forgetting to Import Scanner
Incorrect:
Scanner sc = new Scanner(System.in);
Compilation Error
Correct:
import java.util.Scanner;
Forgetting to Create Scanner Object
Incorrect:
int age = sc.nextInt();
Correct:
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
Mixing nextInt() and nextLine()
Example:
int age = sc.nextInt(); String name = sc.nextLine();
The nextLine() may be skipped because nextInt() leaves a newline character in the input buffer.
Solution:
int age = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
Forgetting to Close Scanner
Good practice:
sc.close();
This releases system resources after input is complete.
Best Practices
-
Import
java.util.Scannerbefore using Scanner. -
Create only one Scanner object for keyboard input.
-
Close the Scanner object after use.
-
Use
nextLine()when reading sentences or names with spaces. -
Display clear prompts before accepting user input.
Key Points to Remember
-
Scanner is used to read user input.
-
It belongs to the
java.utilpackage. -
Import Scanner before using it.
-
next()reads one word. -
nextLine()reads an entire line. -
Always close the Scanner object after use.
Lesson Summary
In this lesson, you learned:
-
What the Scanner class is.
-
How to import and create a Scanner object.
-
How to read integers, decimal numbers, characters, and strings.
-
The difference between
next()andnextLine(). -
Common mistakes and best practices when using Scanner.
Using the Scanner class allows you to create interactive Java programs where users can provide their own input.
Practice Questions
-
What is the Scanner class?
-
Which package contains the Scanner class?
-
How do you create a Scanner object?
-
What is the difference between
next()andnextLine()? -
Which method reads an integer?
-
How do you read a character using Scanner?
-
Why should
sc.close()be used? -
What problem occurs when using
nextInt()followed bynextLine()?
Programming Exercise
Write Java programs to:
-
Read your name and print it.
-
Read your age and print it.
-
Read two numbers and display their sum.
-
Read your percentage and display it.
-
Read your grade and print it.
-
Read your full address using
nextLine().
Assignment
Create a program named StudentRegistration.java that accepts the following details from the user:
-
Student Name
-
Roll Number
-
Age
-
Department
-
Semester
-
Percentage
-
Grade
Display all the entered information in a neatly formatted output.
Next Lesson: Lesson 2.7 – Practice Programs