1. Watch First: Scanner Class Video
Watch the video carefully. Then use the written lesson below to revise the concept, copy the examples, and solve the practice tasks.
2. What Is Scanner in Java?
In Java, the Scanner class is used to read input. It can read text,
numbers, and other values typed by the user.
Before using Scanner, we import it from the Java utility package:
import java.util.Scanner;
Scanner
java.util
Taking input from keyboard
Scanner sc = new Scanner(System.in);
3. The Basic Scanner Program
This is the smallest useful pattern for taking input from the keyboard.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
sc.close();
}
}
Explanation
| Line | Meaning |
|---|---|
import java.util.Scanner; |
Brings the Scanner class into the program. |
Scanner sc = new Scanner(System.in); |
Creates a Scanner object named sc to read keyboard input. |
sc.nextLine() |
Reads one full line of text. |
sc.close() |
Closes the Scanner after use. |
4. Reading Different Types of Input
Scanner has different methods for different data types.
| Input Type | Scanner Method | Example |
|---|---|---|
| Integer | nextInt() |
int age = sc.nextInt(); |
| Decimal number | nextDouble() |
double marks = sc.nextDouble(); |
| Single word | next() |
String city = sc.next(); |
| Full sentence | nextLine() |
String address = sc.nextLine(); |
5. Example: Add Two Numbers
import java.util.Scanner;
class AddTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int sum = a + b;
System.out.println("Sum = " + sum);
sc.close();
}
}
10 and 20.
You should see:
Sum = 30
6. Example: Student Marks Program
This example is closer to school-level Java practice.
import java.util.Scanner;
class StudentMarks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = sc.nextLine();
System.out.print("Enter marks in Computer Science: ");
double cs = sc.nextDouble();
System.out.print("Enter marks in Mathematics: ");
double maths = sc.nextDouble();
System.out.print("Enter marks in English: ");
double english = sc.nextDouble();
double total = cs + maths + english;
double average = total / 3;
System.out.println("Student: " + name);
System.out.println("Total Marks: " + total);
System.out.println("Average Marks: " + average);
sc.close();
}
}
7. Important Scanner Mistake: nextInt() and nextLine()
Beginners often face a problem when they use nextInt() before
nextLine().
nextInt() reads only the number. It does not consume the leftover newline.
So the next nextLine() may read an empty line.
Problem Code
import java.util.Scanner;
class ProblemExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter full name: ");
String name = sc.nextLine();
System.out.println("Age: " + age);
System.out.println("Name: " + name);
sc.close();
}
}
Correct Code
import java.util.Scanner;
class FixedExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
sc.nextLine(); // clears the leftover newline
System.out.print("Enter full name: ");
String name = sc.nextLine();
System.out.println("Age: " + age);
System.out.println("Name: " + name);
sc.close();
}
}
8. Scanner with if-else
Scanner becomes more powerful when combined with conditions.
import java.util.Scanner;
class PassFail {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter marks: ");
int marks = sc.nextInt();
if (marks >= 33) {
System.out.println("Result: Pass");
} else {
System.out.println("Result: Fail");
}
sc.close();
}
}
9. Scanner with Loops
We can also use Scanner inside loops to repeatedly take input.
import java.util.Scanner;
class FiveNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int total = 0;
for (int i = 1; i <= 5; i++) {
System.out.print("Enter number " + i + ": ");
int n = sc.nextInt();
total = total + n;
}
System.out.println("Total = " + total);
sc.close();
}
}
10. Practice Tasks
Task 1: Greeting Program
Take the user's name and city as input. Print:
Hello Champak from Varanasi
Task 2: Area of Rectangle
Take length and breadth as input. Print the area of the rectangle.
Task 3: Simple Interest
Take principal, rate, and time as input. Calculate simple interest.
Task 4: Odd or Even
Take an integer as input. Print whether it is odd or even.
Task 5: Marks Grade
Take marks as input. Print grade using this rule:
- 90 and above: A
- 75 to 89: B
- 60 to 74: C
- 33 to 59: D
- Below 33: Fail
11. Mini Project: Student Report Card
Create a Java program that takes:
- Student name
- Roll number
- Marks in English
- Marks in Mathematics
- Marks in Computer Science
Then print:
- Total marks
- Average marks
- Pass or fail result
import java.util.Scanner;
class ReportCard {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = sc.nextLine();
System.out.print("Enter roll number: ");
int roll = sc.nextInt();
System.out.print("Enter English marks: ");
int english = sc.nextInt();
System.out.print("Enter Mathematics marks: ");
int maths = sc.nextInt();
System.out.print("Enter Computer Science marks: ");
int cs = sc.nextInt();
int total = english + maths + cs;
double average = total / 3.0;
System.out.println("\n--- Report Card ---");
System.out.println("Name: " + name);
System.out.println("Roll Number: " + roll);
System.out.println("Total Marks: " + total);
System.out.println("Average Marks: " + average);
if (english >= 33 && maths >= 33 && cs >= 33) {
System.out.println("Result: Pass");
} else {
System.out.println("Result: Fail");
}
sc.close();
}
}
12. Lesson Summary
Use
import java.util.Scanner;
Use
Scanner sc = new Scanner(System.in);
Use
nextInt(), nextDouble(), next(), or nextLine().
Use
sc.close();
13. Join the Guided CBSE Java Learning System
Learn at your own pace with structured lessons, practice tasks, doubt support, and guided improvement.
- Step-by-step Java lessons
- CBSE-focused examples
- Weekly practice questions
- WhatsApp and Zoom guidance
- Up to 10 support queries per week