Java: User Input

Till now, the lessons discussed how to display outputs to the users. Now is a good time to understand how input can be accepted from them. This process is extremely simple. Several methods can be used. However, the most direct and simple method using a scanner object will be discussed. 

So firstly, the Scanner class must be imported in this manner:

import java.util.Scanner;

After this, a Scanner object has to be created with System.in as an argument.

System.in informs the compiler that some form of input is expected. It is important to understand that the statement mentioned below is vital to accept inputs from users.

Scanner reader = new Scanner(System.in);

Within the Scanner class, several methods can be used to read the inputs provided by users. The methods that are most important include nextInt()nextDouble() and nextLine() which are used to read int, double and String inputs respectively.

To better understand how to utilize these methods, it is a good idea to create a new project in NetBeans with the name InputDemo. The process of creating a new project in NetBeans has already been discussed. Substitute the existing code with the code mentioned below:

import java.util.Scanner;

public class InputDemo {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Please enter an integer: ");
        int exampleInt = input.nextInt();
        System.out.printf("You have typed in %d.%n%n", exampleInt);

        System.out.print("Now, please enter a double: ");
        double exampleDouble = input.nextDouble();
        System.out.printf("You entered %.2f.%n%n", exampleDouble);

        System.out.print("Please type out a string: ");
        input.nextLine();
        String exampleString = input.nextLine();
        System.out.printf("You entered \"%s\".%n%n", exampleString);
    }
}

The java.util.Scanner class was imported, after which, a Scanner object has been created with the name input. The input of the user is of an integer form. To make sure that the compiler can read the integer, the nextInt() method is used. Then, the output was displayed to the users with the printf() method. 

The last few lines work similarly, except that the user is requested to input a double value and not an integer. Moreover, the nextDouble() method is used to read the input.

Then, the user is asked to enter a string, and the nextLine() method is used to read the input.

Take a look at the following method from the previous program:  

input.nextLine();

The nextLine() method was called twice because of how the nextDouble() method functions.

The nextDouble() method can only read a double. However, anytime a user inputs a number, there are high chances of pressing the enter button. The enter key is not very different from the newline character (“\n”) and is not interpreted by the nextDouble()method since it only reads a double. Since the nextDouble() method cannot consume a newline character, an additional nextLine()method is included to consume this newline character.

Try deleting the extra method and run the program again. Additional input cannot be typed as nextLine() method on the next line consumes the preceding newline character, and since no more nextLine() statements exist after this, the program will not expect another input from the user.

Always remember to add an extra nextLine() method every time one nextLine() method is present after the nextDouble() method so that it can consume the last newline character. This suggestion is relevant for the nextInt() method as well. Upon running this program, enter an integer, double, and string when asked, and the program runs as it was intended to run.  

Apart from the aforementioned methods, for every other data type, Java has a corresponding method to read the inputs such as nextByte(), nextShort(), nextLong(), nextFloat() and nextBoolean() methods for reading a byte, short, long, float and boolean value respectively.

However, remember that every method mentioned above expects a specific type of data entered as an input. For example, the nextDouble() method expects to read in a double. So, if a user enters input that does not align with the data type, the data may be converted by the methods into their correct type. However, if they are unable to perform that action, an error message comes up. For instance, if the user inputs a 20 though a type double is expected, the nextDouble() method will convert the integer to a double. However, a string input will cause an error. 

Future lessons will deal with such errors.

Leave a Reply

%d bloggers like this: