Chapter 10 - Java for Beginners Course

Console Input

By: Ambikesh Jha

Java console-based input

In the case of Java, an input can be taken from the console through various classes like Scanner, BufferedReader, etc. In this article, we will focus on how input is taken from the user using a Scanner object. First, we need to cover the System.in reference to proceed with the topic.

The System.in reference

System.in is a static reference field inside the java.lang.System class. It is the standard input stream for reading input data from the console.

It is typically connected to the keyboard. Any input provided from the keyboard for console programs is recorded by System.in and pushed into its stream.

Input taken from the user via a GUI (Graphical User Interface) doesn’t use the System.in input stream.

The Scanner class

The Scanner class is present inside the java.util package. It has various methods that can be used for reading input data of various data types from the console. For example, nextInt() is used for reading an int value as input, nextBoolean() is used for boolean values as input and so on.

Creating Scanner object for user input

Let’s create a Scanner object that takes input from the user through the console.

In the example, run the JavaBasicScannerApp
Scanner scanner = new Scanner(System.in);

System.out.print("Enter first integer: ");
int firstInteger = scanner.nextInt();

System.out.print("Enter second integer: ");
int secondInteger = scanner.nextInt();

System.out.print("Enter operation (add / subtract / multiply) to perform: ");
String operation = scanner.next();

int answer = 0;
if (operation.equals("add")) {
    // add
    answer = firstInteger + secondInteger;
    System.out.println(firstInteger + " + " + secondInteger + " = " + answer);
} else if (operation.equals("subtract")) {
    // subtract
    answer = firstInteger - secondInteger;
    System.out.println(firstInteger + " - " + secondInteger + " = " + answer);
} else if (operation.equals("multiply")) {
    // multiply
    answer = firstInteger * secondInteger;
    System.out.println(firstInteger + " * " + secondInteger + " = " + answer);
} else {
    System.out.println("Invalid operation name provided.");
}

scanner.close();

Output:

Enter first integer: 45
Enter second integer: 23
Enter operation (add / subtract / multiply) to perform: multiply
45 * 23 = 1035

Reading different data types as input from the console

Let’s see an example of reading some data types as input from the console.

In the example, run the JavaScannerInputApp
Scanner scanner = new Scanner(System.in);

//Read a long from console
System.out.print("Enter a long value: ");
long longValue = scan.nextLong();
System.out.println("The entered long value is: " + longValue);

//Read a double from console
System.out.print("Enter a double value: ");
double doubleValue = scan.nextDouble();
System.out.println("The entered double value is: " + doubleValue);

//Read a byte from console
System.out.print("Enter a byte value: ");
byte byteValue = scan.nextByte();
System.out.println("The entered byte value is: " + byteValue);

Output:

Enter a long value: 34567564
The entered long value is: 34567564
Enter a double value: 2.00
The entered double value is: 2.0
Enter a byte value: 12
The entered byte value is: 12

If a wrong input is entered from the keyboard while using Scanner class, we get an exception message like InputMismatchException displayed on the screen. We should handle such errors in our code.

Ensure that you use a try..catch block while using a Scanner class so that you can handle the errors that occur due to mismatched input types.