Parsing Numbers from Input
Convert text to numbers.
Parsing Numbers from Input is a free Java Academy lesson on CoddyKit — lesson 3 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.
Lines Are Always Strings
readLine() always gives you a String, even when the user types 42. To do math, you must parse that text into a number.
Java provides helper methods like Integer.parseInt and Double.parseDouble for exactly this.
Integer.parseInt
Integer.parseInt(String) converts text like "42" into the int value 42.
Once parsed, you can add, multiply, or compare it like any number.
String text = "42";
int n = Integer.parseInt(text);
System.out.println(n + 1);A Runnable Parse Example
Here is a complete, runnable program that parses a hard-coded string and doubles it.
It does not read from System.in, so it runs immediately and prints the result.
public class Main {
public static void main(String[] args) {
String text = "21";
int n = Integer.parseInt(text);
System.out.println("Doubled: " + (n * 2));
}
}Reading Then Parsing
The real pattern: read a line, then parse it. This program reads one number from the console and prints its square.
Because it waits on System.in, it is not auto-runnable here, but it is the everyday pattern.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
System.out.println("Square: " + (n * n));
}
}Trim Before You Parse
Stray spaces break parsing. Integer.parseInt(" 42 ") throws an error, but Integer.parseInt(" 42 ".trim()) works.
It is a good habit to call trim() on the line before parsing.
String raw = " 7 ";
int n = Integer.parseInt(raw.trim());
System.out.println(n);Parsing Decimals
For decimal numbers, use Double.parseDouble. It turns text like "3.14" into a double.
There is also Long.parseLong for very large whole numbers that exceed the int range.
public class Main {
public static void main(String[] args) {
double pi = Double.parseDouble("3.14");
System.out.println(pi * 2);
}
}Several Numbers on One Line
Often a line holds several numbers separated by spaces, like "10 20 30". Use split(" ") to break it into an array of String tokens.
Then parse each token individually.
String[] parts = "10 20 30".split(" ");
int a = Integer.parseInt(parts[0]);
int b = Integer.parseInt(parts[1]);
System.out.println(a + b);Summing Split Numbers
This runnable program splits a line and adds up all the numbers in a loop.
The string is hard-coded so it runs without input, but swap in br.readLine() for real console use.
public class Main {
public static void main(String[] args) {
String line = "5 8 2 10";
String[] parts = line.split(" ");
int sum = 0;
for (String p : parts) {
sum += Integer.parseInt(p);
}
System.out.println("Sum: " + sum);
}
}Bad Input Throws
If the text is not a valid number — like "hello" or an empty string — parseInt throws a NumberFormatException.
You can guard against this with a try/catch so your program does not crash on bad input.
try {
int n = Integer.parseInt("hello");
} catch (NumberFormatException e) {
System.out.println("Not a number!");
}Safe Parsing in Action
Here is a runnable program that handles bad input gracefully instead of crashing.
The catch block prints a friendly message when the text cannot become a number.
public class Main {
public static void main(String[] args) {
String input = "12x";
try {
int n = Integer.parseInt(input);
System.out.println(n);
} catch (NumberFormatException e) {
System.out.println("Invalid number: " + input);
}
}
}Parsing Checklist
When turning input into numbers:
Integer.parseIntfor whole numbers,Double.parseDoublefor decimals.trim()away stray spaces first.split(" ")for many numbers on a line.- Wrap in
try/catchfor safety.
Quick Check
Consider what happens when parsing fails.
Recap
You learned that input lines are strings, parsed with Integer.parseInt or Double.parseDouble. You used trim() and split(" "), and caught NumberFormatException for safety.
Next: closing your streams the right way.
Frequently asked questions
Is the “Parsing Numbers from Input” lesson free?
Yes — the full text of “Parsing Numbers from Input” 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 “Parsing Numbers from Input”?
Convert text to numbers. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parsing Numbers from Input” 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
- Why BufferedReader
- Reading Lines
- Parsing Numbers from Input
- Closing Streams Safely