Reading Lines
readLine and loops.
Reading 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.
The readLine Method
readLine() is the heart of BufferedReader. It reads characters until it hits a line break (Enter), then returns the line as a String.
The returned text does not include the newline character. In this lesson we read single and multiple lines.
String line = br.readLine();Reading One Line
Here is the simplest case: read a name and print a greeting. readLine() blocks (waits) until the user presses Enter.
Because it reads from System.in, this snippet pauses for keyboard input, so it is not auto-runnable here.
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));
String name = br.readLine();
System.out.println("Welcome, " + name + "!");
}
}What readLine Returns at the End
When there is no more input (end of stream, or the user signals EOF), readLine() returns null instead of a String.
This is a crucial signal: checking for null is how we know to stop reading.
String line = br.readLine();
if (line == null) {
System.out.println("No more input.");
}Reading Until EOF
To read every line until input ends, loop while readLine() is not null. This common pattern assigns and checks in one condition.
Each pass gives you the next line, until null stops the loop cleanly.
String line;
while ((line = br.readLine()) != null) {
System.out.println("Got: " + line);
}A Multi-Line Reader
Here is a complete program that echoes back every line you type until the stream ends.
It reads from System.in, so it waits for input and is not auto-runnable, but the loop pattern is exactly what you will reuse.
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));
String line;
while ((line = br.readLine()) != null) {
System.out.println("Echo: " + line);
}
}
}Counting Lines
You can do work inside the loop, like counting. Here we tally how many lines arrive.
Each iteration reads a fresh line and bumps a counter. When input ends, we print the total.
int count = 0;
String line;
while ((line = br.readLine()) != null) {
count++;
}
System.out.println("Lines read: " + count);Empty Lines vs Null
Be careful: an empty line (the user just presses Enter) returns "", an empty String — not null.
So "".equals(line) means a blank line, while line == null means the input is truly finished. They are different things.
if (line == null) {
// input finished
} else if (line.isEmpty()) {
// user pressed Enter on a blank line
}Simulating Lines Without stdin
To see readLine() in action without keyboard input, we can feed a BufferedReader from a StringReader. This is fully runnable.
The string holds two lines separated by \n, and the loop prints each one.
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new StringReader("apple\nbanana"));
String line;
while ((line = br.readLine()) != null) {
System.out.println("Fruit: " + line);
}
}
}Trimming Whitespace
Input often has stray spaces. Use trim() to remove leading and trailing whitespace from a line before using it.
This keeps comparisons and parsing reliable, especially when users add accidental spaces.
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new StringReader(" hello \n"));
String line = br.readLine().trim();
System.out.println("[" + line + "]");
}
}Reading a Fixed Number of Lines
Sometimes you know the count up front. A for loop reads exactly that many lines.
This is common when a problem says “the first number tells you how many lines follow.”
for (int i = 0; i < 3; i++) {
String item = br.readLine();
System.out.println(i + ": " + item);
}Key Habits for readLine
Remember these habits:
- Always check for
nullbefore using a line. trim()when stray spaces could cause bugs.- Empty string is not the same as
null.
Next we will turn these lines into numbers.
Quick Check
What does readLine() return when the input stream has reached its end?
Recap
You learned to read single lines, loop with while ((line = br.readLine()) != null), and tell a blank line ("") from end-of-input (null).
You also saw runnable examples using StringReader. Next: turning text lines into numbers.
Frequently asked questions
Is the “Reading Lines” lesson free?
Yes — the full text of “Reading 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 Lines”?
readLine and loops. 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 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.