Input in Java Using the Scanner Class for ICSE Class 9 (with the nextLine Trap)
ICSE Class 9 Computer Applications notes on taking input with the Scanner class: importing java.util, nextInt, nextDouble, next and nextLine, reading a character, the nextLine skipping problem and its fix, input errors, and solved programs.

Almost every ICSE program begins by reading input from the user. The Scanner class is the standard way to do it in ICSE Computer Applications. It is simple to use, but it has one well-known trap that catches many students in the exam. These notes cover both.
Step 1: Import and create a Scanner
Scanner is in the java.util package:
import java.util.Scanner;
class InputDemo {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in); // System.in is the keyboard
// read input here
}
}Step 2: Read the right data type
nextInt()reads an int:int age = sc.nextInt();nextLong()reads a long:long phone = sc.nextLong();nextDouble()reads a double:double price = sc.nextDouble();nextFloat()reads a float:float f = sc.nextFloat();nextBoolean()reads a boolean (true or false):boolean ok = sc.nextBoolean();next()reads one word, up to a space:String w = sc.next();nextLine()reads the whole line, including spaces:String name = sc.nextLine();
Reading a single character
Scanner has no nextChar(). Read a word and take its first character:
char ch = sc.next().charAt(0);next() vs nextLine()
If the user types Riya Sharma:
sc.next()returns "Riya". It stops at the space.sc.nextLine()returns "Riya Sharma", the whole line.
The nextLine() trap
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter name: ");
String name = sc.nextLine(); // ← seems to be skipped!Why it happens: when you type 15 and press Enter, nextInt() reads only the 15 and leaves the Enter (newline) in the input. The next nextLine() immediately reads that leftover newline, so name becomes an empty string, and the program seems to skip the question.
The fix: add an extra nextLine() to consume the leftover newline:
int age = sc.nextInt();
sc.nextLine(); // consume the leftover Enter
String name = sc.nextLine(); // now waits for the userAnother option is to read all text inputs before the numbers.
Input errors
If the user types text when the program expects a number (for example abc for nextInt()), Java throws an InputMismatchException and the program stops. In ICSE programs you can assume valid input unless the question asks you to validate it.
Solved program: student report
import java.util.Scanner;
class Report {
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 class section (A/B/C): ");
char sec = sc.next().charAt(0);
System.out.print("Enter marks in 3 subjects: ");
int m1 = sc.nextInt(), m2 = sc.nextInt(), m3 = sc.nextInt();
double avg = (m1 + m2 + m3) / 3.0;
System.out.println("Name: " + name + " Section: " + sec);
System.out.println("Total: " + (m1 + m2 + m3) + " Average: " + avg);
}
}Sample run: Riya Sharma, B, 90 85 95 → Total: 270, Average: 90.0
Notice that the name is read first, so there is no nextLine trap. Several numbers can be typed on one line separated by spaces, because nextInt() reads them one by one.
Scanner vs BufferedReader
- Package: Scanner is in
java.util; BufferedReader is injava.io. - Reading numbers: Scanner reads them directly with
nextInt()and similar methods; BufferedReader needsInteger.parseInt(br.readLine()). - Exceptions: Scanner needs no
throws; BufferedReader needsthrows IOException.
For the BufferedReader method, see Data Input Using BufferedReader.
Practice questions
- Which package contains the Scanner class?
- Write a statement to read a character into the variable
grade. - Why does
nextLine()sometimes appear to be skipped afternextInt()?
Answers:
java.utilchar grade = sc.next().charAt(0);- Because
nextInt()leaves the newline in the input, andnextLine()reads that leftover newline as an empty line.
Next, learn Conditional Statements in Java to make decisions with the values you read.
Related Posts
Continue learning with these related articles

User-Defined Methods in Java for ICSE Class 10: Parameters, Return and Overloading
ICSE Class 10 Computer Applications notes on user-defined methods: method prototype and signature, formal and actual parameters, return types, call by value vs call by reference, pure and impure methods, static methods and method overloading, with programs.

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More
Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.