JavaBasic syntax and simple programsSimple programs

Scanning the input

8 seconds read

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 to a file.

Actually, not all programs need to use the standard input. But we will often use it. The typical way to solve programming problems in this course is the following:

1) read data from the standard input (System.in);
2) process data to obtain a result
3) output the result to the standard output (System.out)
The simplest way to obtain data from the standard input is to use the standard class Scanner. It allows a program read values of different types (string, numbers, etc) from the standard 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;

Then you can create an object of this class like:

Scanner scanner = new Scanner(System.in);

Note, System.in is an object that represents the standard input stream in Java.

Now we can read data from the standard input:

String line = scanner.nextLine(); // read a whole line, for example "Hello, Java"

int num = scanner.nextInt(); // read a number, for example 123

double d = scanner.nextDouble(); // read double, for example 123.01

String string = scanner.next(); // read a string (not a line), for example "Hello"

After you call scanner.nextLine() or scanner.nextInt() or something similar, the program will wait for the input data.

Here is an example of a correct input data:

Hello, Java
123 123.01 Hello

The following input example is also correct:

Hello, Java
123
123.01
Hello

It's possible to read a number as a string using scanner.next() or scanner.nextLine() (if the number is in a new line).

Also, the class Scanner has methods for reading values of some other types. See the documentation for details.

The following program read two numbers from the same line and outputs them in reverse order in two different lines.

import java.util.Scanner; // importing scanner from the standard library

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // an object for reading data 

        int num1 = scanner.nextInt(); // read the first number
        int num2 = scanner.nextInt(); // read the second number

        System.out.println(num2); // print the second number
        System.out.println(num1); // print the first number
    }
}

The input/output example 1

The input:

11 12

The output:

12
11

The input/output example 2

The input:

301
402

The output:

402
301

We recommend you to use the class Scanner when solving programming problems in this course. It's one of the most simple ways to get values from the standard input. More complex ways to read data will be studied in other topics.
9 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo