User Input - JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

Chapter 4. User Input

In this chapter the concept of user input will be introduced and incorporated into your Java programming language.

Based from the first simple Java program that we discussed earlier, you were not being asked to provide any form of input for the code execution. The lines of code just displayed the message on the computer screen. In the real world, programming requires a stable communication between the user and the machine. In Java language, this is what we call Input/Output or I/O streams. In this scenario a two-way communication is created where the user provides an input for the computer to process and then produces an output in return.

Getting the User Input

There is a built-in class called Scanner in Java language to easily get the user input. What it does is it acquires information from the input stream, either from the keyboard or a file, and stores in a variable. However, the Scanner class is not part of the core Java language so you need to tell the compiler where to find it. For you to use this, you need to include this line of code at the top of your program, just after the prologue section:

import java.util.Scanner;

Because of the additional statement, the Scanner class is being imported from the java.util package. To call or execute this class in the program, you need to use the following statement:

Scanner InputVariableName = new Scanner(System.in);

Now, the imported class Scanner was called to initialize a variable called InputVariableName (you can change this name to whatever you like but make sure it is not a Java keyword). This is followed by the assignment operator “=” that is in turn followed by the programming code new Scanner(System.in);. This expression creates an object and commands the program to store the value of the user input to InputVariableName. Please take note of the coding convention in variable naming: first letter of the words are always capitalized.

For us to utilize the output stream to show you what you have typed into the program, we need to include the following print out statement:

System.out.println(InputVariableName.nextLine());

The method nextLine() is included here that will instruct the program to return a string value that was inserted into the current line. It also tells the program to wait until it finishes searching for an input, so the program will not advance until you type something and press the Enter key on the keyboard. Let us further modify the println method by including an additive operator:

System.out.println(“You entered “ + InputVariableName.nextLine());

At this point, the output statement contains a textual information that will be printed out together with the Scanner variable. The messages are combined using the additive operator “+” found inside the parentheses. Now, when the user provides an input, like for example “Johnny”, then the program will display “You entered Johnny” on the computer screen.

Our modified program will actually look like this:

From the program above, the message “What is your name?” is displayed to prompt you to enter your name. After you have typed your name and pressed enter (see the text in green located in the Console Section), the program will display your name after the words “You entered ” on the screen.

The introduction of user input as described on this chapter had presented Java environment as a two-way form of communication – between the software programmer and the computer. You, as the user, will provide information for the computer to process through the execution of the lines of Java code. The succeeding chapter will now introduce the concept of variable declaration.