0Pricing
Java Academy · Lesson

Reading Lines

readLine and loops.

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 + "!");
    }
}

All lessons in this course

  1. Why BufferedReader
  2. Reading Lines
  3. Parsing Numbers from Input
  4. Closing Streams Safely
← Back to Java Academy