Reading Numbers and Lines
nextInt, nextLine and pitfalls.
Reading Numbers and Lines is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reading Typed Numbers
Scanner can read numbers directly with methods like nextInt and nextDouble. They convert the next token into the matching primitive type.
Reading an Integer
nextInt() reads the next token and returns it as an int. We use a fixed string here so it runs without typing.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("25");
int age = scanner.nextInt();
System.out.println(age + 1);
scanner.close();
}
}Reading a Double
nextDouble() reads a floating-point value. You can then use it in calculations.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("3.5");
double radius = scanner.nextDouble();
System.out.println(radius * 2);
scanner.close();
}
}Reading Several Numbers
Multiple numeric reads pull tokens one after another, skipping the whitespace between them.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("10 20 30");
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
System.out.println(a + b + c);
scanner.close();
}
}Summing Until Done
Combine hasNextInt() with nextInt() to read an unknown count of numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("5 15 25 35");
int total = 0;
while (scanner.hasNextInt()) {
total += scanner.nextInt();
}
System.out.println(total);
scanner.close();
}
}The Famous nextLine Pitfall
Here is the classic trap: nextInt() reads the number but leaves the newline character in the buffer. The next nextLine() then reads that leftover empty line, not the text you expected.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("42\nHello World");
int n = scanner.nextInt();
String line = scanner.nextLine();
System.out.println("Number: " + n);
System.out.println("Line: [" + line + "]");
scanner.close();
}
}Fixing the Pitfall
The fix is to call an extra nextLine() after nextInt() to consume the dangling newline before reading real line input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("42\nHello World");
int n = scanner.nextInt();
scanner.nextLine();
String line = scanner.nextLine();
System.out.println("Number: " + n);
System.out.println("Line: [" + line + "]");
scanner.close();
}
}Reading Longs and Booleans
Scanner also offers nextLong, nextBoolean, and others. Each reads a token of the matching type.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("9000000000 true");
long big = scanner.nextLong();
boolean flag = scanner.nextBoolean();
System.out.println(big);
System.out.println(flag);
scanner.close();
}
}Mismatched Types Throw
If the next token is not a valid number, nextInt throws an InputMismatchException. Always check with hasNextInt first when unsure.
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("abc");
try {
scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Not an integer!");
}
scanner.close();
}
}Safe Numeric Reading
A robust pattern reads only when the right type is available, otherwise it consumes and discards the bad token.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("x 7 y 9");
int sum = 0;
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
sum += scanner.nextInt();
} else {
scanner.next();
}
}
System.out.println(sum);
scanner.close();
}
}Key Reminders
Use nextInt and nextDouble for numbers, guard with the hasNext family, and remember to consume the leftover newline before switching to nextLine.
Quick Check
Test your number and line reading knowledge.
Recap
You learned to read numbers and lines:
nextInt,nextDouble, andnextLongread typed numbers- Loop with
hasNextIntfor unknown counts - The newline pitfall: call an extra
nextLineafternextInt - Bad tokens throw
InputMismatchException - Guard reads with the
hasNextfamily for safety
Frequently asked questions
Is the “Reading Numbers and Lines” lesson free?
Yes — the full text of “Reading Numbers and Lines” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading Numbers and Lines”?
nextInt, nextLine and pitfalls. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading Numbers and Lines” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reading Input with Scanner
- Reading Numbers and Lines
- Validating User Input
- BufferedReader Alternative