Read a string from standard input in Java
This post will discuss how to read a string from standard input (System.in) using Scanner and BufferedReader in Java. Since a single line of input may contain multiple values, split the line into string tokens.
1. Using Scanner
A simple solution is to use the Scanner class for reading a line from System.in. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.Scanner; class Main { // Read a string from the standard input using `Scanner` public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] tokens = scanner.nextLine().split(" "); System.out.println(Arrays.asList(tokens)); scanner.close(); } } |
We can also use StringTokenizer with the Scanner in place of String.split() to split the input as StringTokenizer is much faster.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.StringTokenizer; import java.util.Scanner; import java.util.List; import java.util.ArrayList; class Main { // Read a string from the standard input using `Scanner` public static void main(String[] args) { List<String> tokens = new ArrayList<>(); Scanner scanner = new Scanner(System.in); StringTokenizer st = new StringTokenizer(scanner.nextLine()); while (st != null && st.hasMoreElements()) { tokens.add(st.nextToken()); } System.out.println(tokens); scanner.close(); } } |
We can also use the Scanner.next() method to convert the resulting tokens into string values, as shown below. Here, hasNext() returns true if this scanner has another token left.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Main { public static void main(String[] args) { List<String> tokens = new ArrayList<>(); Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { tokens.add(scanner.next()); } System.out.println(tokens); scanner.close(); } } |
2. Using BufferedReader
Although Scanner is very convenient for parsing the input, we can speed up things a little by using the BufferedReader and StringTokenizer combo in Java, which is much faster than the Scanner.
In the following program, readLine() reads a line of text which is terminated by any one of a carriage return ('\r'), line feed ('\n'), or a '\r\n'.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; class Main { // Read a string from the standard input using `BufferedReader` public static void main(String[] args) { List<String> tokens = new ArrayList<>(); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); while (st != null && st.hasMoreElements()) { tokens.add(st.nextToken()); } System.out.println(tokens); } catch (IOException e) { System.out.println(e); } } } |
That’s all about reading a string from standard input in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)