Skip to content
Computer ApplicationsICSE Class 9 · Data Processing in Java

Initialisation, Parameters and Scanner Input Methods in Java with Input Tracing (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

A Java program can get data by initialisation (values written in the code before execution), by parameters (values passed when a method is called) or through an input stream (values typed while the program runs, read with a Scanner object). Scanner methods match the data type: nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), next(), nextLine() and next().charAt(0). After a number is read, nextLine() returns the rest of that line, which may be empty.

Notice that nextInt() leaves the Enter behind, so the next nextLine() reads an empty rest of the line; add an extra nextLine() to clear it.

Illustration drawn for QuestionBuddy.

Understand Initialisation, Parameters and Scanner Input Methods with Input Tracing

Three ways to give data.
1. Initialisation (data before execution): int a = 10; The value is fixed in the program and changes only if the code is edited.
2. Parameters (data at the time of execution): values supplied when a method is called. For void show(int l, int b) the call obj.show(7, 3); passes 7 and 3. In BlueJ a dialog box asks for the values when such a method is run.
3. Input stream (data entry during execution): the program waits and the user types values, which a Scanner object reads: Scanner sc = new Scanner(System.in);

Scanner methods.

  • nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(): read numbers of those types.
  • next(): reads the next word (token) as a String, stopping at a space or a new line.
  • nextLine(): reads the rest of the current line, including spaces, as a String.
  • next().charAt(0): reads the next word and takes its first character; there is no nextChar().

Tokens. Numbers and words in the input are separated by spaces or new lines. Each next... call consumes one token and leaves the rest for the next call.

Reading a line after a number. nextInt(), nextDouble() and next() do not consume the new line typed after the value. A nextLine() that follows then reads the empty remainder of that line. Fixes: read the text first, add an extra sc.nextLine(); to throw away the remainder, or use next() for one-word text.

Choosing a method. The method must match the variable type: a decimal typed for nextInt() causes a run-time error, and a whole line with spaces needs nextLine().

Neighbouring topics: packages, the three kinds of errors and comments are on the next page of this chapter.

Initialisation, Parameters and Scanner Input Methods with Input Tracing: key facts

Three ways
Initialisationvalues in the code
Parametersvalues passed at the call
Input streamvalues typed during execution
Compare them by when the data arrives.
Scanner methods
  • nextInt() int
  • nextDouble() double
  • next() word
  • nextLine() line
  • next().charAt(0) char
There is no nextChar().
Line trap
nextInt() leaves the Enter | the next nextLine() reads an empty string
Read the text first or add an extra nextLine().

Initialisation, Parameters and Scanner Input Methods with Input Tracing in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Three ways to give data

Suppose a city bus has a fixed route number written on its board, the driver is handed the start time when the trip is assigned, and a passenger tells the conductor the stop where to get off during the journey.

Match each with initialisation, parameters and input stream.

Try it first, then show the solutionSolution
  1. The route number is fixed beforehand: initialisation.
  2. The start time is handed over when the trip begins: parameters.
  3. The stop is told during the journey: input stream.

Answer: Route number is initialisation, start time is a parameter and the passenger's stop is an input.

Memory hook: Before, at the call, during.

Tricks to remember Initialisation, Parameters and Scanner Input Methods with Input Tracing

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • No nextChar

    To read a character use next().charAt(0).

    Use it when: Reading a char.

  • Next stops at a space

    next() reads one word; nextLine() reads the whole line.

    Use it when: Choosing a string method.

  • Number, then line, trouble

    nextInt() then nextLine() reads an empty line.

    Use it when: The classic trap.

Initialisation, Parameters and Scanner Input Methods with Input Tracing: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Method for a long · 1 mark · MCQBoard level

Which Scanner method reads a long value?
(a) nextInt() (b) nextLong() (c) nextDouble() (d) next()

Try it first, then show the solutionSolution
  1. The method name follows the data type.

Answer: (b) nextLong()

Example 2: Tokens and lines · 3 marks · Short answerHarder board level

The input is:

12 7.5 Amit Rao
Delhi

Find the output:

int a = sc.nextInt();
double d = sc.nextDouble();
String w = sc.next();
String rest = sc.nextLine();
String city = sc.nextLine();
System.out.println(a + "|" + d + "|" + w + "|" + rest + "|" + city);
Try it first, then show the solutionSolution
  1. nextInt() reads 12, nextDouble() reads 7.5 and next() reads the word Amit. (1 mark)
  2. nextLine() reads the rest of the first line, which is " Rao" with its leading space. (1 mark)
  3. The second nextLine() reads the whole next line: Delhi. Output: 12|7.5|Amit| Rao|Delhi. (1 mark)

Answer: 12|7.5|Amit| Rao|Delhi

Example 3: The empty line · 3 marks · Short answerHarder board level

The input is 9, Enter, Goa, Enter. Find the output:

int n = sc.nextInt();
String s = sc.nextLine();
String s2 = sc.nextLine();
System.out.println(n + " [" + s + "] " + s2);
Try it first, then show the solutionSolution
  1. nextInt() reads 9 and leaves the Enter in the input. (1 mark)
  2. The first nextLine() reads the remaining empty part of that line: s = "". (1 mark)
  3. The second nextLine() reads Goa. Output: 9 [] Goa. (1 mark)

Answer: 9 [] Goa

Example 4: Case study: classify the data entry · 4 marks · Case-basedHOTS / competency

A school program has the statements:

int total = 100;
obj.show(7, 3);
int marks = sc.nextInt();

where void show(int l, int b) prints "Area = " + l * b.
(i) Name the way in which each value is supplied. (2)
(ii) What does obj.show(7, 3) print? (1)
(iii) Which of the three needs a Scanner object? (1)

Try it first, then show the solutionSolution
  1. (i) total = 100 is initialisation; 7 and 3 are parameters passed at the call; marks is an input stream value read at run time.
  2. (ii) l * b = 7 x 3 = 21, so it prints Area = 21.
  3. (iii) The input stream (marks = sc.nextInt()).

Answer: (i) initialisation, parameters, input stream (ii) Area = 21 (iii) the input stream

Example 5: Reading a character · 3 marks · Short answerHarder board level

The user types Yes and presses Enter. Find the output:

char c = sc.next().charAt(0);
System.out.println(c + " " + (int) c);
Try it first, then show the solutionSolution
  1. next() reads the word Yes and charAt(0) takes its first character Y. (1 mark)
  2. The code of Y is 65 + 24 = 89 because A is 65 and Y is the 25th letter. (1 mark)
  3. Output: Y 89. (1 mark)

Answer: Y 89

Where students lose marks in Initialisation, Parameters and Scanner Input Methods with Input Tracing

The slips examiners see most often, and what to write instead.

  • Mistake: Using a non-existent nextChar()

    Other languages are remembered.

    Wrong working

    char c = sc.nextChar();

    Right working

    char c = sc.next().charAt(0);

    Do this: Scanner has no nextChar().

    Usually costs: 1 mark

  • Mistake: nextLine() straight after nextInt()

    The leftover Enter is forgotten.

    Wrong working

    The name is always empty

    Right working

    Read the text first or add an extra sc.nextLine();

    Do this: Remember the leftover Enter.

    Usually costs: 2 marks

  • Mistake: Wrong method for a decimal

    Type and method differ.

    Wrong working

    double x = sc.nextInt();

    Right working

    double x = sc.nextDouble();

    Do this: Use the matching method.

    Usually costs: 1 mark

Exam tips

  • Write the Scanner object statement before any input method.
  • For comparisons, state when the data arrives: before, at the call or during execution.
  • Show the input tokens separated by lines when tracing.
  • Mention that nextInt() does not consume the Enter key.

Practice questions on Initialisation, Parameters and Scanner Input Methods with Input Tracing

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which method reads a single word from the input?
    (a) nextLine() (b) next() (c) nextInt() (d) charAt()

    Hint

    It stops at a space.

    Answer

    (b) next()

  2. Q2 · 2 marks · Very short answerBoard level

    Write statements to read a full name that may contain spaces and then an age as an int.

    Hint

    Read the text first.

    Answer

    Scanner sc = new Scanner(System.in);
    String name = sc.nextLine();
    int age = sc.nextInt();

  3. Q3 · 2 marks · Very short answerHarder board level

    Name the Scanner method that reads each: (i) 12 (ii) 12.5 (iii) the letter Q (iv) the text New Delhi.

    Hint

    Match the type.

    Answer

    (i) nextInt() (ii) nextDouble() (iii) next().charAt(0) (iv) nextLine()

  4. Q4 · 3 marks · Short answerHarder board level

    The input is 5 6. Find the output:

    int a = sc.nextInt();
    int b = sc.nextInt();
    System.out.println(a + b + " " + a + b);
    Hint

    The first + adds numbers; later ones join.

    Answer

    11 56 (a + b = 11 is added first; then " " is joined, then 5 and 6 are joined as text)

  5. Q5 · 3 marks · Short answerHOTS / competency

    A program reads String name = sc.next(); and for the input Mary Ann it prints only Mary. Explain and correct.

    Hint

    Where does next() stop?

    Answer

    next() stops at the first space, so only Mary is read. Use String name = sc.nextLine(); to read the whole line (and read it before any number).

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Which method reads a float?Show answer

    nextFloat()

  2. 2.Which call gives a char?Show answer

    next().charAt(0)

  3. 3.Which method reads a whole line?Show answer

    nextLine()

  4. 4.Which method reads one word?Show answer

    next()

  5. 5.What are the three ways of supplying data?Show answer

    Initialisation, parameters and input stream.

  6. 6.Does nextInt() consume the Enter?Show answer

    No.

Initialisation, Parameters and Scanner Input Methods with Input Tracing: frequently asked questions

Can I type two numbers on one line?

Yes. Tokens may be separated by spaces or new lines.

Why is there a dialog in BlueJ for some methods?

A method with parameters needs its values; BlueJ asks for them before running it.

Is there nextBoolean()?

Scanner has such a method, but the Class IX list names the numeric methods, next(), nextLine() and next().charAt(0).