Java ProgramsBasics & I/ORead String From User

Read String From User in Java

beginner·  Basics & I/O  ·  I/O

Problem

Scanner.nextLine() reads an entire line of input, including spaces, unlike next() or nextInt() which stop at the first whitespace.

Read a line of text from the console and print it.

Input
Hello Java
Output
You entered: Hello Java

Java Program

Java
import java.util.Scanner; public class ReadStringFromUser { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a line of text: "); String value = sc.nextLine(); // reads the full line, including spaces System.out.println(value); // echo back — this compiler doesn't echo typed input itself System.out.println("You entered: " + value); } }

Output

Enter a line of text: Hello Java You entered: Hello Java

Core Logic

Scanner reads the entire typed line as one String, spaces included.

How It Works
  1. 1new Scanner(System.in) creates a reader attached to the console.
  2. 2System.out.print(...) shows a prompt without a trailing newline.
  3. 3sc.nextLine() reads everything typed up to the newline, keeping any spaces between words.
  4. 4System.out.println(value) echoes the value right after the prompt, since most online compilers don't echo typed input themselves the way a real terminal does.
  5. 5A separate labeled line then confirms what was read.
Typing Hello Java makes sc.nextLine() return the full string "Hello Java", spaces included.
💡

Key Point: nextLine() is the only Scanner method that reads spaces as part of the value — next() would stop at the first space and return just "Hello".

Key Concepts

ScannernextLine()console input

Approach 2: BufferedReader

Java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class ReadStringBufferedReader { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a line of text: "); String value = br.readLine(); // already a String, no parsing needed System.out.println(value); // echo back — this compiler doesn't echo typed input itself System.out.println("You entered: " + value); } }

Output

Enter a line of text: Hello Java You entered: Hello Java

Core Logic

BufferedReader.readLine() reads a whole line directly as a String, with no parsing step needed since the target type is already text.

How It Works
  1. 1new BufferedReader(new InputStreamReader(System.in)) wraps standard input in a buffered character reader.
  2. 2br.readLine() reads the whole line and returns it as a String directly.
  3. 3No conversion step is needed here, unlike the integer or double versions — the value read is already the target type.
  4. 4The method signature adds throws IOException, since readLine() can throw a checked exception.
Typing Hello Java makes br.readLine() return "Hello Java" directly.
💡

Key Point: For plain text input, BufferedReader is actually simpler than Scanner — there's no parsing step to add, since readLine() already returns a String.

Key Concepts

BufferedReaderreadLine()

Related Programs