TutorialsICSEClass 9Computer Applications

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.

9 min read
Input in Java Using the Scanner Class for ICSE Class 9 (with the nextLine Trap)

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 user

Another 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 in java.io.
  • Reading numbers: Scanner reads them directly with nextInt() and similar methods; BufferedReader needs Integer.parseInt(br.readLine()).
  • Exceptions: Scanner needs no throws; BufferedReader needs throws IOException.

For the BufferedReader method, see Data Input Using BufferedReader.

Practice questions

  1. Which package contains the Scanner class?
  2. Write a statement to read a character into the variable grade.
  3. Why does nextLine() sometimes appear to be skipped after nextInt()?

Answers:

  1. java.util
  2. char grade = sc.next().charAt(0);
  3. Because nextInt() leaves the newline in the input, and nextLine() reads that leftover newline as an empty line.

Next, learn Conditional Statements in Java to make decisions with the values you read.