Reading Input with the Scanner Class (ICSE Class 10 Java)
Updated 17 October 2026 · Fact-checked
To read values while the program runs, import java.util.Scanner, create Scanner in = new Scanner(System.in); and call a method matching the type: nextInt(), nextDouble(), nextLong(), nextFloat(), nextShort(), next() for one word, nextLine() for a whole line, and next().charAt(0) for one character.
Understand Scanner Methods for Reading Values
Three ways to give data to a program. Initialization (values fixed in the code before execution), parameters (values passed when a method is called) and input streams (values typed while the program runs). The Scanner class handles the last.
Setting up. Scanner is in the package java.util, so the first line of the file is import java.util.Scanner; (or import java.util.*;). Then create the object: Scanner in = new Scanner(System.in); where System.in is the keyboard stream.
Methods in the syllabus.
nextShort(),nextInt(),nextLong(): whole numbersnextFloat(),nextDouble(): real numbersnext(): one word, stops at a space, tab or newlinenextLine(): the rest of the current line, including spacesnext().charAt(0): Scanner has no method for a single char; read a word and take the character at index 0
Spaces and lines. Numbers and next() skip white space before them and stop at the next white space, so 12 8.5 typed on one line, or on two lines, is read the same way by nextInt() and nextDouble(). nextLine() reads up to the end of the line.
The nextLine trap. After nextInt() the Enter key press is still waiting in the input. A following nextLine() takes that leftover and returns an empty string. Fix: read the number, call nextLine() once to throw away the rest of the line, or read the number with Integer.parseInt(in.nextLine()).
Matching types. If the user types text such as abc where nextInt() expects a number, the program stops with an exception at run time (InputMismatchException). Choose the method that matches the variable: double d = in.nextDouble();, String s = in.nextLine();.
Prompting. Print a message with System.out.print before reading so the user knows what to type.
Scanner Methods for Reading Values: key facts
- Setup
- import java.util.Scanner; | Scanner in = new Scanner(System.in);
- Both lines are needed; the import comes before the class.
- Methods by type
int nextInt() long nextLong() short nextShort() float nextFloat() double nextDouble() word next() line nextLine() - The method decides the type returned.
- Character input
- char c = in.next().charAt(0);
- next() reads a word
- charAt(0) takes its first character
- Takes the first character of the word typed.
- Three data entry ways
Initialization before execution Parameters at the time of method call Input stream during execution - A frequent 3-mark question.
How to answer Scanner Methods for Reading Values questions
Use this routine for input-reading questions.
- 1Write the typed input as a list of tokens separated by white space, noting the line breaks.
- 2Take each Scanner call in order and match it to the next token (next, nextInt, nextDouble) or to the rest of the line (nextLine).
- 3Check that each token fits the type; if not, state that the program stops with a run-time exception.
- 4Remember the leftover line break after a number when nextLine() follows.
- 5Then evaluate the output statements using the stored values.
Scanner Methods for Reading Values 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.
Reading a whole line versus a word
Suppose a school form asks for a student's full name, such as "Rita Sen", and the program must keep both words together.
Which of next() and nextLine() should the program use, and what would the other one return for "Rita Sen"?
Try it first, then show the solutionSolution
- nextLine() returns everything up to the end of the line: Rita Sen.
- next() would stop at the first space and return only Rita.
Answer: Use nextLine(); next() would give only Rita.
Memory hook: next() = one word, nextLine() = whole line.
Tricks to remember Scanner Methods for Reading Values
Short memory aids. Say each one aloud once, then use it on a question the same day.
Name the type
next + Int, Double, Long...: the method name is "next" plus the type.
Use it when: Choosing the method.
char has no method
There is no nextChar(): use next().charAt(0).
Use it when: Character input.
Enter is left behind
A number leaves the Enter key waiting, so nextLine() after it reads nothing.
Use it when: The skipped-input trap.
Scanner Methods for Reading Values: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Statements to read values · 2 marks · Short answerBoard level
Write statements to input an integer n, a double price and a character grade using the Scanner object in.
Try it first, then show the solutionSolution
- int n = in.nextInt(); and double price = in.nextDouble(); (1 mark)
- char grade = in.next().charAt(0); because Scanner has no method that reads a char directly. (1 mark)
Answer: int n = in.nextInt(); double price = in.nextDouble(); char grade = in.next().charAt(0);
Example 2: Matching input to calls · 3 marks · Short answerHarder board level
The input typed is: 12 8.5 Rita Sen
Find the output:
int a = in.nextInt();
double d = in.nextDouble();
String f = in.next();
String l = in.next();
System.out.println(l + ", " + f + " " + (a * d));Try it first, then show the solutionSolution
- Tokens are 12, 8.5, Rita, Sen. a = 12, d = 8.5, f = "Rita", l = "Sen". (1 mark)
- a * d = 12 * 8.5 = 102.0 (int times double gives double). (1 mark)
- The text joins "Sen", ", ", "Rita", " " and 102.0. (1 mark)
Answer: Sen, Rita 102.0
Example 3: The skipped nextLine · 3 marks · Short answerHarder board level
The input has two lines:
5
Hello World
Find the output:
int n = in.nextInt();
String s = in.nextLine();
String t = in.nextLine();
System.out.println("[" + s + "]");
System.out.println("[" + t + "]");Try it first, then show the solutionSolution
- nextInt() reads 5 but leaves the line break after it. (1 mark)
- The first nextLine() reads the rest of the first line, which is empty, so s = "". (1 mark)
- The second nextLine() reads Hello World, so t = "Hello World". Output shows the brackets. (1 mark)
Answer: []
[Hello World]
Example 4: Case study: area and perimeter · 4 marks · Case-basedHOTS / competency
A student writes:
import java.util.Scanner;
public class Rect {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Length: ");
int l = in.nextInt();
System.out.print("Breadth: ");
double w = in.nextDouble();
System.out.println("Area = " + l * w);
System.out.println("Perimeter = " + 2 * (l + w));
}
}(i) The user types 6 and then 2.5. State the full output. (2)
(ii) What happens if the user types 6 and then abc? (1)
(iii) Why is the import line needed? (1)
Try it first, then show the solutionSolution
- (i) Prompts print without a line break. Area: 6 * 2.5 = 15.0. Perimeter: 2 * (6 + 2.5) = 2 * 8.5 = 17.0. Output: Length: Breadth: Area = 15.0, then Perimeter = 17.0 (the typed numbers also appear on the screen).
- (ii) nextDouble() cannot read abc, so the program stops with a run-time exception (InputMismatchException) after Breadth.
- (iii) Scanner is in the package java.util, which is not imported automatically.
Answer: (i) Area = 15.0 and Perimeter = 17.0 (ii) run-time exception, program stops (iii) Scanner belongs to java.util
Example 5: Reading a character · 3 marks · Short answerHarder board level
The input is: Yes
Write the output:
char c = in.next().charAt(0);
System.out.println(c);
System.out.println((int) c);
System.out.println(c == 'Y');(ASCII code of A is 65.)
Try it first, then show the solutionSolution
- next() returns "Yes"; charAt(0) is Y. (1 mark)
- Y is the 25th letter, so its code is 65 + 24 = 89. (1 mark)
- c == 'Y' compares the characters and gives true. (1 mark)
Answer: Y
89
true
Where students lose marks in Scanner Methods for Reading Values
The slips examiners see most often, and what to write instead.
Mistake: Forgetting the import or the Scanner object
Without them the compiler cannot find Scanner or in.
Wrong working
int n = in.nextInt(); (no import, no Scanner in = ...)
Right working
import java.util.Scanner; ... Scanner in = new Scanner(System.in); int n = in.nextInt();
Do this: Write both lines first in every program.
Usually costs: 1 mark
Mistake: Using nextChar()
The method does not exist in Scanner.
Wrong working
char c = in.nextChar();
Right working
char c = in.next().charAt(0);
Do this: Use next().charAt(0).
Usually costs: 1 mark
Mistake: Expecting nextLine() right after nextInt() to read the next line
The leftover line break is read first, giving an empty string.
Wrong working
After 5 and Hello World: s = "Hello World"
Right working
s = "" ; a second nextLine() gives Hello World
Do this: Add one extra in.nextLine(); after the number.
Usually costs: 1 to 2 marks
Exam tips
- Always begin a program answer with the import line.
- Use nextLine() for names with spaces and next() for single words.
- In a dry run, list the tokens first and match each Scanner call to a token.
- Quote the syllabus list when asked for methods: nextShort, nextInt, nextLong, nextFloat, nextDouble, next, nextLine.
Practice questions on Scanner Methods for Reading Values
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
In which package is the Scanner class present?
(a) java.lang (b) java.io (c) java.util (d) java.awtHint
The import line starts java.
Answer
(c) java.util
Q2 · 1 mark · Very short answerBoard level
Name the Scanner method that reads an entire line of text including spaces.
Hint
next + Line.
Answer
nextLine()
Q3 · 2 marks · Short answerHarder board level
The input is: 3 4
Write the output:int a = in.nextInt(), b = in.nextInt(); System.out.println(a + b); System.out.println(a + "" + b);Hint
One line adds, the other joins.
Answer
7
34Q4 · 4 marks · Long answerHarder board level
Write a complete program that inputs two integers and displays their average as a real number.
Hint
Divide by 2.0 to keep the fraction.
Answer
import java.util.Scanner;
public class Avg {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
double avg = (a + b) / 2.0;
System.out.println("Average = " + avg);
}
}
For input 7 and 10 it prints Average = 8.5.Q5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): The statement char c = in.next().charAt(0); stores only the first character of the word typed.
Reason (R): next() reads one word and charAt(0) returns the character at index 0 of that word.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
What does index 0 mean?
Answer
(a) Both are true and R explains A.
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.Which line creates the Scanner object?Show answer
Scanner in = new Scanner(System.in);
2.How do you read one character?Show answer
in.next().charAt(0)
3.What does next() stop at?Show answer
The first white space (space, tab or line break).
4.Which method reads a double?Show answer
nextDouble()
5.What is the import statement for Scanner?Show answer
import java.util.Scanner;
6.What is System.in?Show answer
The standard input stream, the keyboard.
Scanner Methods for Reading Values: frequently asked questions
Why does my program skip the name input?
The number read before it left the Enter key waiting, so nextLine() returned an empty string. Call nextLine() once more to discard the rest of that line.
Can Scanner read a char directly?
No nextChar() exists; use next().charAt(0). The syllabus methods are nextShort, nextInt, nextLong, nextFloat, nextDouble, next and nextLine. (Scanner also has nextBoolean(), but it is outside the listed syllabus methods.)
What is the difference between initialization and input?
Initialization gives a variable a value in the code before execution; input reads the value from the user while the program is running.