Input in Java ICSE Class 10 Computer Applications
Updated 14 October 2026 · Fact-checked
A Java program can get data by initialisation (values written in the code before execution), by parameters (values passed when the method is run) or by input streams during execution using the Scanner class from java.util: nextInt(), nextDouble(), next(), nextLine() and next().charAt(0). ICSE Class 10 also asks syntax, runtime and logical errors and the two comment styles.
What Input in Java is about
Almost every Section B program begins by accepting data with the Scanner class, and examiners regularly deduct marks for wrong input statements: in 2024 many could not accept String and char input for the courier program, and in 2025 some initialised a string instead of accepting it. Error questions in Section A also come from this chapter: in 2025 a cone program used a variable before giving it a value, and candidates wrongly called the error logical.
The syllabus asks for: initialisation (data before execution), parameters (data at the time of execution), input streams with Scanner methods (nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), next(), nextLine(), next().charAt(0)), an introduction to packages, types of errors (syntax, runtime, logical) and single-line and multi-line comments.
Weightage: CISCE publishes no chapter weightage. Section A (40 marks) is compulsory: 1-mark MCQs and 2-mark short answers including output and error questions; in Section B you answer any four of six 15-mark programming questions written in Java.Source: CISCE ICSE Computer Applications (661) specimen paper structure for 2027
Key concepts
- Three ways to give data to a program
(1) Initialisation: data is written in the program itself before execution: int a = 10; double rate = 8.5; The values are fixed unless the program is edited.
(2) Parameters (arguments): data is supplied at the time of execution, when the method is called. In BlueJ, if main is written as void main(int a, int b), a dialog box asks for a and b when you run it.
class Add
{
void main(int a, int b)
{
System.out.println("Sum = " + (a + b));
}
}
Running with 6 and 9 prints Sum = 15.(3) Input stream: data is entered by the user during execution, through the keyboard, using the Scanner class: the program pauses, the user types, and the value is read.
Worked example
(2 marks) Distinguish between initialisation and input through Scanner.
Try it first, then show the solutionSolution
- Initialisation: values are fixed in the program before execution - 1 mark.
- Scanner: values are entered by the user during execution, so the program works for any data - 1 mark.
Answer: In initialisation the data is written into the program before it runs (int a = 10;), so it is the same every time; with Scanner the user enters data during execution, so the program can work on different values each time.
- Packages and the Scanner class
A package is a group of related classes. java.lang is the default package, imported automatically; it contains System, String, Math and the wrapper classes such as Integer and Character. java.util contains utility classes such as Scanner, and must be imported: import java.util.Scanner; (or import java.util.*;) at the top of the program, before the class.
Creating a Scanner object linked to the keyboard:
Scanner sc = new Scanner(System.in);Methods (as listed in the syllabus):
nextShort() - short; nextInt() - int; nextLong() - long; nextFloat() - float; nextDouble() - double;
next() - one word (up to a space);
nextLine() - a whole line including spaces;
next().charAt(0) - one character (the first character of the next word).Prompt the user before reading: System.out.print("Enter age: "); int age = sc.nextInt();
Worked example
(2 marks) Write statements to accept a full name with spaces and a grade letter.
Try it first, then show the solutionSolution
- String name = sc.nextLine(); reads the whole line including spaces - 1 mark.
- char grade = sc.next().charAt(0); reads one character - 1 mark.
Answer: String name = sc.nextLine();
char grade = sc.next().charAt(0);- next() vs nextLine() and a common trap
next() reads characters up to the next space, so for the input 'Ravi Kumar' it returns only Ravi. nextLine() reads the rest of the line, so it returns Ravi Kumar.
Trap: after nextInt() or nextDouble(), the Enter key's line break is left in the input. A nextLine() called next will read that empty remainder and return "". Solutions: read the String first, or add an extra sc.nextLine(); to clear the line before reading the String.
Example:
int age = sc.nextInt();
sc.nextLine();
String city = sc.nextLine();Worked example
(2 marks) The user types Neha Sharma. What do next() and nextLine() each return if called first?
Try it first, then show the solutionSolution
- next() stops at the space: Neha - 1 mark.
- nextLine() reads the whole line: Neha Sharma - 1 mark.
Answer: next() returns "Neha"; nextLine() returns "Neha Sharma".
- Types of errors
(1) Syntax (compile-time) errors: breaking the grammar rules of Java, found by the compiler; the program does not compile. Examples: missing semicolon, misspelt keyword (Int), undeclared variable, mismatched brackets, assigning a double to an int without a cast, using a local variable before giving it a value.
(2) Runtime errors: the program compiles but stops abnormally while running. Examples: dividing an integer by zero (ArithmeticException), using an array index outside its range, entering text when nextInt() expects a number (InputMismatchException).
(3) Logical errors: the program compiles and runs but gives a wrong result because the logic is wrong. Examples: using + instead of *, the wrong formula, a wrong loop condition, 1 / 2 giving 0 in an expression.
Note: in the 2025 cone program, a = 22.0/7*r*l; came before l was given a value. A local variable must be assigned before use, so the compiler rejects it: a syntax (compile-time) error, not a logical error.
Worked example
(2 marks) Identify the type of error: (a) int x = 10 / 0; (b) area = l + b; for the area of a rectangle.
Try it first, then show the solutionSolution
- (a) Compiles but crashes on execution (division by zero): runtime error - 1 mark.
- (b) Runs but gives a wrong answer (should be l * b): logical error - 1 mark.
Answer: (a) Runtime error (ArithmeticException: / by zero). (b) Logical error.
- Comments
Comments are notes in a program that the compiler ignores; they explain the code to readers.
Single-line comment: starts with // and runs to the end of the line.
// calculate the total
Multi-line comment: starts with /* and ends with */ and can span several lines.
/* This program accepts marks
and prints the average */
(A documentation comment /** ... */ is also used for documentation.)Good comments, together with a variable description table, help the examiner follow a Section B answer.
Worked example
(1 mark) Which is a valid multi-line comment?
(a) // text //
(b) /* text */
(c) <!-- text -->
(d) textTry it first, then show the solutionSolution
- Multi-line comments use /* to start and */ to end.
Answer: (b) /* text */
Input in Java in real life
Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Choosing the right Scanner method
Suppose a school's admission program must accept a student's full name, age, height in metres, house initial and the city.
Choose the Scanner method for each.
Try it first, then show the solutionSolution
- Full name with spaces: nextLine().
- Age: nextInt(); height: nextDouble().
- House initial: next().charAt(0); city (one word): next().
Answer: name = sc.nextLine(); age = sc.nextInt(); height = sc.nextDouble(); house = sc.next().charAt(0); city = sc.next();
Memory hook: Line for names, Int for counts, Double for measures, charAt(0) for a letter.
Types of errors
Imagine a recipe app: one version will not install because of a typing mistake in its code; another installs but crashes when 0 servings are entered; a third runs but shows double the sugar needed.
Match each with syntax, runtime or logical error.
Try it first, then show the solutionSolution
- Will not install/compile: syntax error.
- Crashes during use: runtime error.
- Runs but wrong result: logical error.
Answer: First: syntax error; second: runtime error; third: logical error.
Memory hook: Won't compile, won't finish, won't be right.
Parameters
Suppose a teacher runs a BlueJ method void main(int marks) and types 78 in the dialog box that appears.
How is the data being supplied?
Try it first, then show the solutionSolution
- The value is not written in the code (not initialisation).
- It is not read by Scanner during execution.
- It is passed as an argument when the method is invoked: a parameter.
Answer: Through a parameter: the value 78 is passed to main at the time of execution.
Memory hook: Parameters arrive with the call.
Topics in this chapter
Input in Java: syntax and key facts
- Scanner set-up
- import java.util.Scanner; … Scanner sc = new Scanner(System.in);
- Import before the class.
- Scanner methods
- nextShort, nextInt, nextLong, nextFloat, nextDouble, next(), nextLine(), next().charAt(0)
- next(): one word; nextLine(): whole line.
- Errors
- Syntax: compiler rejects; Runtime: crashes while running; Logical: wrong output
- Comments
- // single line; /* multi-line */
- Ignored by the compiler.
How the board asks Input in Java
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| MCQ (Section A) | Which package contains Scanner / Character (2025: java.lang for Character); identify a comment or error type. | java.lang is default (String, Math, wrappers); java.util needs import (Scanner). |
| Error correction (2 marks) | Specify the type of error and correct the program (2025 cone program). | If the compiler would refuse it (variable used before assignment, missing semicolon), it is a syntax error. |
| Program (15 marks) | Accept data with Scanner, process it and print results (every Section B program). | Prompt, read with the right method, store in the variable names given in the question. |
Tricks to remember Input in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
util for input
Scanner is a utility: java.util. Everything basic (System, Math, String, wrappers) is in java.lang.
Use it when: Package questions.
S-R-L
Syntax stops compiling, Runtime stops running, Logic stops being right.
Use it when: Types of errors.
Input in Java: board-level solved questions
Ordered from board level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: Package of Character class · 1 mark · MCQBoard level
The Character class methods are found in the package:
(a) java.util
(b) java.lang
(c) java.awt
(d) java.io
Try it first, then show the solutionSolution
- Wrapper classes such as Character are in java.lang, the default package.
Answer: (b) java.lang
Example 2: Reading a character · 1 mark · MCQHarder board level
Which statement correctly accepts a single character?
(a) char c = sc.nextChar();
(b) char c = sc.next().charAt(0);
(c) char c = sc.nextLine();
(d) char c = sc.charAt(0);
Try it first, then show the solutionSolution
- Scanner has no nextChar(); read a word and take its first character.
Answer: (b) char c = sc.next().charAt(0);
Example 3: Error type in the cone program · 2 marks · Short answerHarder board level
Specify the type of error and correct it:
class area
{
double area(double r, double h)
{
double l, a;
a = 22.0 / 7 * r * l;
l = Math.sqrt(r * r + h * h);
return a;
}
}
Try it first, then show the solutionSolution
- l is used before it is assigned; Java rejects a local variable that may not be initialised: syntax (compile-time) error.
- Correct by computing l first: l = Math.sqrt(r * r + h * h); a = 22.0 / 7 * r * l;
Answer: Syntax (compile-time) error: variable l might not have been initialised. Corrected body:
double l, a;
l = Math.sqrt(r * r + h * h);
a = 22.0 / 7 * r * l;
return a;
Example 4: Classify errors · 2 marks · Short answerHarder board level
Classify each as syntax, runtime or logical error:
(a) System.out.println("Hi") (missing semicolon)
(b) int n = sc.nextInt(); when the user types 'ten'
(c) avg = a + b + c / 3; for the average of three numbers
Try it first, then show the solutionSolution
- (a) Compiler error: syntax.
- (b) Program stops while running: runtime (InputMismatchException).
- (c) Runs but only c is divided: logical.
Answer: (a) Syntax error (b) Runtime error (c) Logical error; correct: avg = (a + b + c) / 3.0;
Example 5: The nextLine trap · 2 marks · Short answerHOTS / competency
A student writes:
int roll = sc.nextInt();
String name = sc.nextLine();
When run, the program skips the name. Why? Correct it.
Try it first, then show the solutionSolution
- nextInt() leaves the line break after the number; nextLine() reads that empty remainder.
- Add sc.nextLine(); after nextInt(), or read the name first.
Answer: nextInt() reads only the number and leaves the Enter in the input, so nextLine() returns an empty string. Correct:
int roll = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
Example 6: Report card program · 15 marks · Long answerHarder board level
Write a program to accept a student's name (with spaces), roll number and marks in three subjects (decimals allowed), and print the name, total and average. Use comments.
Try it first, then show the solutionSolution
- import java.util.Scanner;
class Report
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// name first, so no nextLine trap
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Roll number: ");
int roll = sc.nextInt(); - System.out.print("Marks in three subjects: ");
double m1 = sc.nextDouble();
double m2 = sc.nextDouble();
double m3 = sc.nextDouble(); - /* total and average */
double total = m1 + m2 + m3;
double avg = total / 3; - System.out.println("Name: " + name + ", Roll: " + roll);
System.out.println("Total = " + total);
System.out.println("Average = " + avg);
}
} - Trace: Asha Rao, 12, marks 80 70.5 90.5: total = 241.0; average = 241.0 / 3 = 80.33333333333333.
Answer: Sample run:
Name: Asha Rao
Roll number: 12
Marks in three subjects: 80 70.5 90.5
Name: Asha Rao, Roll: 12
Total = 241.0
Average = 80.33333333333333
Example 7: Shop bill with char input · 15 marks · Long answerHOTS / competency
Write a program to accept the item name (one word), quantity, rate and a customer type ('M' for member, any other letter for non-member). Members get 10% discount. Print the amount payable.
Try it first, then show the solutionSolution
- import java.util.Scanner;
class ShopBill
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Item: ");
String item = sc.next();
System.out.print("Quantity: ");
int qty = sc.nextInt();
System.out.print("Rate: ");
double rate = sc.nextDouble();
System.out.print("Type (M/N): ");
char type = sc.next().charAt(0); - double amount = qty * rate;
if (type == 'M')
amount = amount - amount * 10 / 100; - System.out.println(item + " : amount payable = " + amount);
}
} - Trace: Pens, 4, 25.0, M: amount = 100.0; discount 10.0; payable 90.0.
- Trace: Pens, 4, 25.0, N: payable 100.0.
Answer: Sample run:
Item: Pens
Quantity: 4
Rate: 25
Type (M/N): M
Pens : amount payable = 90.0
Case-study practice (Section B style)
Read the situation, then answer every part. In the board paper this is a 10-mark question split into 2 + 3 + 5 marks.
Debugging a fee program
Imagine Arjun's program to accept a student's name and monthly fee and print the yearly fee:
class Fee
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int fee = sc.nextInt();
String name = sc.nextLine();
int yearly = fee + 12;
System.out.println(name + " pays " + yearly)
}
}
(i) · 2 marks
Find two syntax errors.
Answer
Answer: (1) Scanner is used without import java.util.Scanner; (2) the println statement has no semicolon.
1 mark each.
(ii) · 3 marks
Find the logical error and correct it.
Answer
Answer: yearly = fee + 12 adds instead of multiplying; correct: int yearly = fee * 12;
Identification 2 marks; correction 1 mark.
(iii) · 5 marks
Explain why the name is not read, and rewrite the input part correctly with prompts.
Answer
Answer: nextInt() leaves the line break, so nextLine() reads an empty line. Corrected:
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Monthly fee: ");
int fee = sc.nextInt();Explanation 2 marks; corrected code 3 marks.
Where the marks are
How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.
Specify the type of error in the cone program and correct it. (2 marks) (2 marks)
| Step | Marks |
|---|---|
| Syntax/compile-time error: l used before initialisation. | 1 |
| Corrected order of statements. | 1 |
Program: accept name, roll and three marks; print total and average. (15 marks) (15 marks)
| Step | Marks |
|---|---|
| import and Scanner object. | 2 |
| Prompts and nextLine() for the name. | 2 |
| nextInt() / nextDouble() for roll and marks. | 3 |
| Total and average with decimal division. | 4 |
| Output with labels. | 2 |
| Comments / variable description. | 2 |
Where students lose marks in Input in Java
The slips examiners see most often, and what to write instead.
Mistake: Unable to accept String and char input
The 2024 examiners reported this in the courier program.
Do this: String: sc.nextLine() or sc.next(); char: sc.next().charAt(0).
Usually costs: 1 to 2 marks of 15.
Mistake: Initialising data instead of accepting it
In 2024 (EvenPal) and 2025 (Super string) candidates used sample values instead of accepting input.
Do this: If the question says 'accept', read with Scanner.
Usually costs: 1 to 2 marks.
Mistake: Calling a compile-time error logical
The 2025 examiners noted candidates called the cone program's error logical.
Do this: If the compiler would refuse the code, it is a syntax (compile-time) error.
Usually costs: 1 mark.
Mistake: Forgetting import java.util.Scanner
Without it, Scanner is an unknown class.
Do this: Write the import as the first line of every program that uses Scanner.
Usually costs: 1 mark.
Practice questions on Input in Java
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Which method reads a whole line including spaces?
(a) next()
(b) nextLine()
(c) nextInt()
(d) charAt()Hint
Line.
Answer
(b) nextLine()
Q2 · 1 mark · MCQBoard level
Scanner belongs to the package:
(a) java.lang
(b) java.util
(c) java.io only
(d) java.mathHint
Utility.
Answer
(b) java.util
Q3 · 1 mark · MCQBoard level
Dividing an integer by zero causes a:
(a) syntax error
(b) logical error
(c) runtime error
(d) commentHint
While running.
Answer
(c) runtime error
Q4 · 2 marks · Very short answerBoard level
Distinguish between a syntax error and a logical error with an example each.
Hint
Compile vs result.
Answer
A syntax error breaks Java's rules and is caught by the compiler, e.g. a missing semicolon. A logical error lets the program run but gives wrong output, e.g. writing area = l + b; instead of l * b.
Q5 · 2 marks · Very short answerHarder board level
Write statements to accept a long phone number and a float weight.
Hint
nextLong, nextFloat.
Answer
long phone = sc.nextLong();
float weight = sc.nextFloat();Q6 · 2 marks · Short answerHOTS / competency
The user types: 25 Delhi Public School. What do these give, in order? int a = sc.nextInt(); String s = sc.next(); String t = sc.nextLine();
Hint
next stops at space.
Answer
a = 25; s = "Delhi"; t = " Public School" (the rest of the line, starting with the space).
Q7 · 2 marks · Very short answerHarder board level
Why is java.lang not imported in programs, while java.util is?
Hint
Default.
Answer
java.lang is imported automatically into every Java program because it holds essential classes (System, String, Math, wrappers); other packages such as java.util must be imported explicitly.
Q8 · 15 marks · Long answerBoard level
Write a program to accept the length of a side of a square and its unit as a word (e.g. cm), and print the area and perimeter with the unit.
Hint
next() for the unit.
Answer
import java.util.Scanner;
class Square
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Side: ");
double s = sc.nextDouble();
System.out.print("Unit: ");
String u = sc.next();
System.out.println("Area = " + (s * s) + " sq " + u);
System.out.println("Perimeter = " + (4 * s) + " " + u);
}
}
Trace with 6 and cm: Area = 36.0 sq cm; Perimeter = 24.0 cm.Q9 · 2 marks · Short answerHOTS / competency
A program prints the average of 5 and 6 as 5. Name the error type and explain a likely cause.
Hint
int division.
Answer
A logical error: the average was probably computed as (a + b) / 2 with int variables, so 11 / 2 gave 5. Use (a + b) / 2.0 to get 5.5.
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.Default package of Java?Show answer
java.lang
2.Read one character?Show answer
sc.next().charAt(0)
3.Error caught by the compiler?Show answer
Syntax (compile-time) error.
4.Single-line comment symbol?Show answer
//
5.Three ways of giving data?Show answer
Initialisation, parameters, input stream (Scanner).
Best order to study it
- Initialisation, parameters and input streams.
- Packages: java.lang and java.util.
- Scanner object and the eight listed methods.
- next() vs nextLine() and the nextLine trap.
- Syntax, runtime and logical errors.
- Comments.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- Initialisation: before execution; parameter: at execution; Scanner: during execution.
- import java.util.Scanner; Scanner sc = new Scanner(System.in);
- nextInt, nextDouble, nextLong, nextShort, nextFloat, next, nextLine, next().charAt(0).
- Syntax error: won't compile; runtime: crashes; logical: wrong output.
- // one line; /* many lines */
Input in Java: frequently asked questions
Is there a nextChar() method?
No. Use sc.next().charAt(0).
Can I use BufferedReader instead of Scanner?
The syllabus names the Scanner class; use Scanner unless the question says otherwise.
Do I have to close the Scanner?
Not for ICSE answers; sc.close() is optional good practice at the end.
Based on: CISCE ICSE 2027 Computer Applications syllabus; CISCE Analysis of Pupil Performance ICSE 2025: Computer Applications (with the 2025 board paper)