Type Here to Get Search Results !

User input in java using Scanner.

0

 User input in java using Scanner.


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.

Post a Comment

0 Comments