Theory:Scanning the input
The standard input is a stream of data going into a program. It is supported by the operating system. By default, the standard input obtains data from the keyboard input but it's possible to redirect it from a file.
Actually, not all programs need to use the standard input. But we will often use it here to help you master your programming skills! The typical way to solve programming problems is the following:
- Read data from the standard input (System.in);
- Process data to obtain a result;
- Output the result to the standard output (System.out).
This type of code challenge can be easily tested by different formats of input data, and for this reason, we will use them a lot.
# Reading data with a scanner
The simplest way to obtain data from the standard input is to use the standard class
Scanner
. It allows a program to read values of different types (string, numbers, etc) from the standard input. In this topic, we will consider reading data from the input.To use this class you should add the following import statement to the top of your file with the source code.
import java.util.Scanner;
1Then you add the following construction after the import:
Scanner scanner = new Scanner(System.in);
1With this line, we create an object of
Scanner
class, that enables us to use its methods. We will learn more about creating objects in other topics.System.in
indicates that the program will read text that you type in the standard input. For now, you will always require this line exactly.There are two ways to read strings with a
Scanner
class. If your input is an integer number or a single word, you can read the data usingnext()
method. As an example, the following code fragment reads the user’s name and prints hello message:String name = scanner.next(); System.out.println("Hello, " + name + "!");
1
2
3For instance, the user's name is James. The output of the program will be the following:
Hello, James!
1If the user's input is an integer number like 123, the program will output this number. Note that the
next()
method will store 123 or another integer number as a string, even if we know that this string consists of a number.Hello, 123!
1
提示
There are more specialized methods for reading other types of input values. In this topic, we only consider reading strings.
But, if the user prints a compound name like Erich Maria, the program will output only the first word:
Hello, Erich!
In this case, you'll need another method, a nextLine()
method, which reads and outputs the whole line:
Hello, Erich Maria!
As you may notice, the next()
method reads one word only and doesn't include any whitespace. By contrast, the nextLine()
method includes all space characters it encounters.
提示
Note that in Java whitespace includes not only the space character, but mostly everything that looks empty when printed: a tab, the newline character, and other non-printing characters.
In this article, we are dealing with space and newline characters: technically, we produce a corresponding character when pressing Enter and starting a new line. The term "whitespace" is used to refer to either of them. The more correct term to refer to what we’ve called “word” is token: it is a piece of text surrounded by whitespace. We can say now that the next()
method finds and returns the next token, while the nextLine()
reads all data till the end of the current line.
Now you can read a whole word and even a whole line invoking these two methods. To invoke both of them correctly, it is important to know the difference between them.
# Reading a multiline input
Reading multiline input may still be a bit tricky: you should take into account the position of the cursor and the reading methods behavior.
Let’s investigate this process with an example:
|This is a simple
multiline input,
that is being read
2
3
4
5
| is a position of the cursor before reading the input.
If we invoke the next()
method, the program will read the input till the whitespace, as indicated by the color blue:
This| is a simple
multiline input,
that is being read
2
3
4
5
After invoking the nextLine()
method the program reads the whole line starting from the whitespace. This data is indicated by a green color. The nextLine()
places the cursor at the beginning of a new line (if there is such a line in your input):
This is a simple
|multiline input,
that is being read
2
3
4
5
Then, let's invoke the next()
method two times. The first input is indicated by an orange color. You may see that the position of the cursor is right after the word and before the whitespace:
This is a simple
multiline| input,
that is being read
2
3
4
5
Now we invoke the next()
method again. The program outputs the second word in the line without whitespace. It doesn't even matter how many space characters are there, because the next()
method will skip the whitespace until it finds the next token.
The second input is indicated by light blue color. As you may see, the position of the cursor is still at the current line right before the new line and after the comma:
This is a simple
multiline input,|
that is being read
2
3
4
5
Here is a tricky thing about the nextLine()
method that also shows a major difference between the next()
and the nextLine()
methods. As you already know, the program will read input from the position of the cursor till the new line (again, if there is such a line in your input). In this example the cursor is located before the new line: thus, the nextLine()
method will return an empty line ("") and place the cursor at the beginning of a new line.
This is a simple
multiline input,
|that is being read
2
3
4
5
To sum up, let's look at the code as a whole and consider the variables we have just read:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String word1 = scanner.next(); // "This"
String line1 = scanner.nextLine(); // " is a simple"
String word2 = scanner.next(); // "multiline"
String word3 = scanner.next(); // "input,"
String line2 = scanner.nextLine(); // ""
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
This example may seem artificial, but it should help you to catch the difference between these two methods. Remember that usually the variables are named in a more expressive way.
# Conclusion
We can read data from the standard input with a special Scanner
class. The next()
and the nextLine()
methods will help you to read strings. Both of them are used for getting input, but they act differently. The next()
method can read the input only till the whitespace while the nextLine()
method reads the input till the end of the whole line.
We recommend you to use the class Scanner
when solving programming problems. It is one of the simplest ways to get values from the standard input. More complex ways to read data will be discussed in further topics.