In this Java tutorial, you will learn how to read a string from console input entered by the user using the Scanner class. You will also learn when to use nextLine(), how it differs from next(), and what alternatives are available for console input in Java.
Java Read String from Console Input Using Scanner
To read a string from Console as input in Java applications, you can use Scanner class along with the InputStream, System.in.
For a full line of text, including spaces, use scanner.nextLine(). This is the usual choice when you want to read a name, sentence, address, message, or any other string entered by the user through the keyboard.
When you are developing console applications using Java, it is very important that you read input from user through console.
In this tutorial, we will learn how to prompt user to input a string and then read the string from console input.
Java Scanner nextLine() Example to Read a Full String
1. Read string from console input
In this example, we shall define a Scanner with the input stream, System.in.
System.in creates a standard input stream which is already open and ready to supply input data.
Scanner is simple text scanner which can parse primitive types and strings using regular expressions.
So, passing System.in to Scanner allows us to parse or read string from standard input stream, which is console.
Example.java
import java.util.Scanner;
/**
* An example program to read a String from console input in Java
*/
public class Example {
public static void main(String[] args) {
System.out.print("Enter a string : ");
Scanner scanner = new Scanner(System. in);
String inputString = scanner. nextLine();
System.out.println("String read from console is : \n"+inputString);
}
}
When the program is run, the execution waits after printing the prompt string "Enter a string : ", where the user would enter a string something like "hello world" as shown in the following console window.
The program prints the string read from the console input in the next step. The whole output in the console would be as shown in the following.
Output
Enter a string : hello world
String read from console is :
hello world
Why nextLine() Is Used for Reading a String with Spaces in Java
The method nextLine() reads the complete line of input until the user presses Enter. Therefore, if the user types hello world, the complete text hello world is stored in the string variable.
The method next() is different. It reads only the next token, usually up to the next whitespace. If the user types hello world, next() reads only hello.
| Scanner method | Input typed by user | Value read | Best used for |
|---|---|---|---|
nextLine() | hello world | hello world | Full string or sentence |
next() | hello world | hello | Single word input |
Java Program to Read a Single Word String from Console
If you only want to read one word from the console, you may use scanner.next(). This is useful for inputs such as a username, city code, option name, or any value that does not contain spaces.
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter one word: ");
String word = scanner.next();
System.out.println("Word entered: " + word);
}
}
Output
Enter one word: Java Programming
Word entered: Java
In the above run, only Java is read because next() stops reading at the first space.
Reading Multiple String Lines from Console in Java
Sometimes you may need to read more than one line of text from the user. In that case, call nextLine() once for each line you want to read.
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first line: ");
String firstLine = scanner.nextLine();
System.out.print("Enter second line: ");
String secondLine = scanner.nextLine();
System.out.println("First line: " + firstLine);
System.out.println("Second line: " + secondLine);
}
}
Output
Enter first line: Java is simple
Enter second line: Scanner reads input
First line: Java is simple
Second line: Scanner reads input
Fixing the Common Scanner Issue After Reading Numbers
A common problem occurs when nextLine() is used after methods such as nextInt(), nextDouble(), or next(). The previous input method may leave the newline character in the input buffer. Then nextLine() reads that leftover newline and appears to skip the actual user input.
To handle this, call an extra nextLine() after reading the number, before reading the actual string line.
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
int age = scanner.nextInt();
scanner.nextLine(); // consumes the leftover newline
System.out.print("Enter full name: ");
String name = scanner.nextLine();
System.out.println("Age: " + age);
System.out.println("Name: " + name);
}
}
Output
Enter age: 20
Enter full name: Ravi Kumar
Age: 20
Name: Ravi Kumar
Using BufferedReader to Read a String from Console in Java
Scanner is simple and beginner-friendly. Another common way to read console input in Java is BufferedReader with InputStreamReader. This approach is also suitable when you want to read input line by line.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Example {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string: ");
String input = reader.readLine();
System.out.println("String entered: " + input);
}
}
Output
Enter a string: Welcome to Java
String entered: Welcome to Java
Using Console readLine() for Java Console Input
Java also provides the Console class for console-based input and output. You can use System.console() to get the console object and then call readLine() to read a string.
However, System.console() may return null in some environments, such as certain IDE run windows. For that reason, Scanner is often easier for learning and simple examples.
import java.io.Console;
public class Example {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("Console is not available.");
return;
}
String input = console.readLine("Enter a string: ");
System.out.println("String entered: " + input);
}
}
About System.in in Java Console Programs
In the context of reading something from the console, System class provides a means to access standard input through one of its fields, in.
in field is a Stream (to be specific, its a InputStream), which is declared public static and final. Hence, one can use in directly without any initialization.
Typically, this InputStream corresponds to keyboard input or another input source specified by the host environment or user.
Should You Close Scanner When It Uses System.in?
In small examples, you may see code that does not close the Scanner created with System.in. Closing this scanner also closes the underlying standard input stream. If your program needs to read from the console again later, closing it too early can cause problems.
For short standalone programs, either approach is commonly seen. In larger applications, manage input resources carefully and avoid closing System.in while the application still needs console input.
Common Mistakes While Reading a String from Console in Java
- Using
next()when the input may contain spaces. UsenextLine()for a full line. - Using
nextLine()immediately afternextInt()without consuming the leftover newline. - Expecting
System.console()to work in every IDE. It may returnnulldepending on the environment. - Closing a
ScanneronSystem.intoo early when more console input is needed later. - Not printing a clear prompt before waiting for user input.
Java Console String Input FAQs
How to read a string from console input in Java?
Create a Scanner object using new Scanner(System.in) and call nextLine() to read a full string line from the console.
How to read a string with spaces in Java?
Use scanner.nextLine(). It reads the complete line until the user presses Enter, so input such as hello world is read as one string.
What is the difference between next() and nextLine() in Java Scanner?
next() reads only the next token up to whitespace, while nextLine() reads the entire line of input including spaces.
Why does nextLine() skip input after nextInt()?
nextInt() reads the number but leaves the newline character in the input buffer. The next nextLine() may read that leftover newline. Call an extra nextLine() after nextInt() before reading the actual string.
Can BufferedReader read a string from console in Java?
Yes. You can use BufferedReader with InputStreamReader(System.in) and call readLine() to read a string line from the console.
Editorial QA Checklist for Java Console String Input Tutorial
- The main example uses
ScannerwithSystem.into read user input from the console. - The tutorial clearly explains why
nextLine()is preferred for strings that contain spaces. - The difference between
next()andnextLine()is shown with an input example. - The newline issue after
nextInt()is explained with a working fix. - Alternative approaches such as
BufferedReaderandConsole.readLine()are presented without making them look required for simple programs.
Conclusion: Reading String Input from Java Console
In this Java Tutorial, we have learned to use Scanner class to read a String from console input in Java.
For most beginner console programs, Scanner scanner = new Scanner(System.in) with scanner.nextLine() is the simplest way to read a full string. Use next() only when you need a single word, and use the extra nextLine() fix when reading a line after numeric input.
TutorialKart.com