Skip to content
Computer ApplicationsICSE Class 9 · Basic Structure of a Class in Java

Writing Programs with Scanner Input and Tracing Their Outputs (ICSE Class 9 Java)

Updated 18 October 2026 · Fact-checked

A program that takes input follows the prescribed order: comment, import java.util.*;, class header, main(), declaration of variables, creation of the Scanner object, accepting values with methods such as nextInt() or nextDouble(), the set of statements and the output. The method must match the variable's type, and a division of two ints drops the fraction unless one operand is made decimal.

Notice that dividing int by int drops the fraction, while making one operand decimal (3.0) keeps it.

Illustration drawn for QuestionBuddy.

Understand Writing Programs with Scanner Input and Tracing Their Outputs

Order of a program with input.
1. // comment about the program
2. import java.util.*;
3. class header and braces
4. main() header and braces
5. Declaration of variables
6. Scanner sc = new Scanner(System.in);
7. Prompt with print() and accept the value with sc.nextInt(), sc.nextDouble() and so on
8. Statements for processing
9. Output with println()

Choosing the input method. The method follows the type of the variable: int uses nextInt(), double uses nextDouble(), a word uses next(), a whole line uses nextLine(). A decimal typed for nextInt() causes a run-time error.

Integer division trap. In (m1 + m2 + m3) / 3 all operands are int, so the fraction is dropped. Divide by 3.0 or use a double variable: (m1 + m2 + m3) / 3.0.

Common processing patterns.

  • Average: sum / count (decimal division).
  • Splitting units: total / 60 gives whole hours and total % 60 gives the remaining minutes.
  • Discount: amount * rate / 100 gives the discount.
  • Swapping with a third variable: t = a; a = b; b = t;

Presenting results. Show a label with the value ("Total fare = Rs " + total). The + sign joins the label and the number.

Tracing a program with input. Write the typed values in a table against the variables, then update the table after each statement, and finally write the lines exactly as they appear on the screen.

Neighbouring topics: the class header, main() and layout rules are on the previous page of this chapter; the list of Scanner methods and errors is in the chapter Data Processing in Java.

Writing Programs with Scanner Input and Tracing Their Outputs: key facts

Order of parts
  • comment
  • import
  • class header
  • main()
  • declarations
  • Scanner object
  • input
  • statements
  • output
The CISCE structure.
Average of three
  • (a + b + c) / 3.0
  • divide by a decimal
  • or store in a double
Integer division otherwise drops the fraction.
Hours and minutes
  • hours = total / 60
  • minutes = total % 60
  • total in minutes
Integer division and remainder.
Swap using a third variable
  • t = a
  • a = b
  • b = t
Three assignments in this order.

How to answer Writing Programs with Scanner Input and Tracing Their Outputs questions

Use this routine to write a program that takes input.

  1. 1Decide the inputs, outputs and the formula.
  2. 2Choose a type for each variable and the matching Scanner method.
  3. 3Write the structure: import, class, main(), declarations, Scanner object.
  4. 4Prompt with print() and read each value.
  5. 5Write the statements and print the result with a label; check integer division.

Writing Programs with Scanner Input and Tracing Their Outputs 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.

Fare calculator

Suppose a bus ticket machine asks for the number of tickets and the price of one ticket and then prints the amount to pay.

Which values are inputs and which are outputs? If 4 tickets cost Rs 35 each, what is printed?

Try it first, then show the solutionSolution
  1. Inputs: number of tickets and price.
  2. Output: amount = 4 x 35.
  3. The program multiplies the two values.

Answer: Amount = Rs 140.

Memory hook: Input, process, output.

Tricks to remember Writing Programs with Scanner Input and Tracing Their Outputs

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

  • I-P-O

    Input, Process, Output: the three jobs of a simple program.

    Use it when: Planning a program.

  • Type decides the method

    int uses nextInt(), double uses nextDouble().

    Use it when: Choosing a Scanner method.

  • Make one operand decimal

    Divide by 3.0 to keep the fraction.

    Use it when: Averages.

Writing Programs with Scanner Input and Tracing Their Outputs: solved examples

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

Example 1: Scanner method · 1 mark · MCQBoard level

Which Scanner method reads a decimal number into a double variable?
(a) nextInt() (b) next() (c) nextDouble() (d) nextLine()

Try it first, then show the solutionSolution
  1. A double needs a method that returns a double.

Answer: (c) nextDouble()

Example 2: Four results from two inputs · 3 marks · Short answerHarder board level

Trace the program for the input 9 and 4:

import java.util.*;
class Calc
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int s = a + b, d = a - b, m = a * b;
double q = (double) a / b;
System.out.println(s + " " + d + " " + m + " " + q);
}
}
Try it first, then show the solutionSolution
  1. a = 9 and b = 4. (1 mark)
  2. s = 13, d = 5, m = 36. (1 mark)
  3. q = 9.0 / 4 = 2.25. Output: 13 5 36 2.25. (1 mark)

Answer: 13 5 36 2.25

Example 3: Minutes to hours · 3 marks · Short answerHarder board level

A program reads the total minutes and prints hours and minutes:

int total = sc.nextInt();
int h = total / 60;
int m = total % 60;
System.out.println(h + " h " + m + " min");

Give the output for the input 275.

Try it first, then show the solutionSolution
  1. 275 / 60 = 4 because 4 x 60 = 240 and 5 x 60 = 300 is too large. (1 mark)
  2. 275 % 60 = 275 - 240 = 35. (1 mark)
  3. The line joins the numbers with the labels: 4 h 35 min. (1 mark)

Answer: 4 h 35 min

Example 4: Case study: shop bill · 4 marks · Case-basedHOTS / competency

A shop program reads the price per item as a double and the quantity as an int:

double price = sc.nextDouble();
int qty = sc.nextInt();
double amount = price * qty;
double discount = amount * 10 / 100;
double net = amount - discount;
System.out.println(amount + " " + discount + " " + net);

For the input 50 and 6:
(i) State the amount. (1)
(ii) State the discount and the net amount. (2)
(iii) Why can the quantity be an int but the price a double? (1)

Try it first, then show the solutionSolution
  1. (i) amount = 50.0 x 6 = 300.0.
  2. (ii) discount = 300.0 x 10 / 100 = 3000.0 / 100 = 30.0; net = 300.0 - 30.0 = 270.0. The output line is 300.0 30.0 270.0.
  3. (iii) A quantity is a whole number, but a price may have paise, so it needs a decimal type.

Answer: (i) 300.0 (ii) discount 30.0 and net 270.0 (iii) quantity is whole, price may be decimal

Example 5: Swapping values · 2 marks · Short answerHarder board level

Trace:

int a = 12, b = 20, t;
t = a;
a = b;
b = t;
System.out.println(a + " " + b);
Try it first, then show the solutionSolution
  1. t = 12; a becomes 20. (1 mark)
  2. b becomes t = 12. Output: 20 12. (1 mark)

Answer: 20 12

Where students lose marks in Writing Programs with Scanner Input and Tracing Their Outputs

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

  • Mistake: Wrong Scanner method

    The type of the variable is ignored.

    Wrong working

    double p = sc.nextInt();

    Right working

    double p = sc.nextDouble();

    Do this: Match the method to the type.

    Usually costs: 1 mark

  • Mistake: Integer division in an average

    Both operands are int.

    Wrong working

    (a + b + c) / 3 gives a whole number

    Right working

    (a + b + c) / 3.0 keeps the fraction

    Do this: Divide by a decimal.

    Usually costs: 1 to 2 marks

  • Mistake: No prompt before input

    The user does not know what to type.

    Wrong working

    int n = sc.nextInt();

    Right working

    System.out.print("Enter n: "); int n = sc.nextInt();

    Do this: Always print a prompt first.

    Usually costs: 1 mark

Exam tips

  • Write the import line and the Scanner object every time input is used.
  • Choose variable types before writing the statements.
  • Show a sample run in your answer with the typed values.
  • Check integer division in every average or percentage.

Practice questions on Writing Programs with Scanner Input and Tracing Their Outputs

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 statement creates the keyboard input object?
    (a) Scanner sc = new Scanner(System.in); (b) Scanner sc = Scanner(System.in); (c) new Scanner sc(System.in); (d) sc = Scanner;

    Hint

    Use new.

    Answer

    (a) Scanner sc = new Scanner(System.in);

  2. Q2 · 2 marks · Very short answerBoard level

    Write statements to read two integers x and y from the keyboard, with prompts.

    Hint

    Prompt with print().

    Answer

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter x: ");
    int x = sc.nextInt();
    System.out.print("Enter y: ");
    int y = sc.nextInt();

  3. Q3 · 2 marks · Short answerHarder board level

    Give the output:

    int m1 = 78, m2 = 85, m3 = 92;
    double avg = (m1 + m2 + m3) / 3.0;
    System.out.println(avg);
    Hint

    Add first, then divide by a decimal.

    Answer

    85.0 (78 + 85 + 92 = 255 and 255 / 3.0 = 85.0)

  4. Q4 · 3 marks · Short answerHarder board level

    A program has int avg = (m1 + m2) / 2; and for m1 = 75 and m2 = 80 it shows 77 while the true average is 77.5. Explain and correct.

    Hint

    Which types are involved?

    Answer

    Both operands are int, so 155 / 2 drops the fraction and gives 77, and avg is an int anyway. Correct: double avg = (m1 + m2) / 2.0; which gives 77.5.

  5. Q5 · 4 marks · Short answerHOTS / competency

    Write a program that reads the price of 1 kg of rice (double) and a weight in grams (int) and prints the cost. For the input 80 and 250, state the output if the program prints only the cost.

    Hint

    Cost = price * grams / 1000.

    Answer

    import java.util.*;
    class RiceCost
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.print("Price per kg: ");
    double price = sc.nextDouble();
    System.out.print("Weight in grams: ");
    int g = sc.nextInt();
    double cost = price * g / 1000;
    System.out.println(cost);
    }
    }
    For 80 and 250 the output is 20.0 (80.0 x 250 = 20000.0 and 20000.0 / 1000 = 20.0).

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 line makes Scanner available?Show answer

    import java.util.*;

  2. 2.Method to read an int?Show answer

    nextInt()

  3. 3.Method to read a decimal into a double?Show answer

    nextDouble()

  4. 4.Which expression gives whole hours from minutes m?Show answer

    m / 60

  5. 5.Which expression gives the leftover minutes?Show answer

    m % 60

  6. 6.How to avoid integer division in an average?Show answer

    Divide by a decimal such as 3.0.

Writing Programs with Scanner Input and Tracing Their Outputs: frequently asked questions

Is a prompt compulsory?

It is not a compiler rule, but programs without prompts lose clarity and marks.

Do I need System.in?

Yes. Scanner(System.in) connects the Scanner object to the keyboard.

Can I read all values with nextLine()?

Only strings. Numbers are read with nextInt(), nextDouble() and so on.