You are on page 1of 7

[CS202 / Computer Programming 2]

1
[Getting Input]

Getting Keyboard Input


Since we have already written a program that can output text, we are now going to make
our more interactive by allowing the user input text in our program. In this module, we are
going to discuss two ways of getting input, the first one is using Scanner class and the other
one is using JOptionPane class.
At the end of the lesson, the student should be able to:
Create an Java program that accepts input.
Use the Scanner class to get input through console.
Use the JOptionPane class to get input from the keyboard through a graphical user
interface.

Using Scanner Class


In this section we are going to read user input in a console application using Scanner class.
Heres an example of program using Scanner class:

The output of the program will depend on the user input. The program will ask the user to
enter his name and after entering the name it will display the Your name is and the name
that was entered. Below is the sample output:

Let us try to explain each line of code:


Line 1 or this statement:

Course Module
indicates that we want to use the class Scanner, which is part of java.util package. Java
provided us Application Programming Interface (API) containing hundreds of predefined
classes that are very useful in writing programs. These classes are organized into what we
call packages.
Line 3 and 4,

we have already discussed this in the previous lesson, this means we declare a class named
GetInputUsingScanner and we also declare the main method.
The statement,

we declare variable scan with Scanner type. Don't worry about what the syntax for now,
we will cover more about this later. The statement will allow us to use all the features
attributed to Scanner class.
Line 8 will just display the text on the screen asking for the name of the user.

In the line 9,

the scan.nextLine() is a method call that gets input from the user and will return a String
value. The returned value will be saved to the variable name, which will be used to display
in the output statement,

Aside from nextLine(), there are other methods (in Scanner class) that can be called to read
input from the user, these are the following:
nextByte(), returns byte value
nextShort(), returns short value
nextInt(), returns int value
nextLong(), returns long value
nextFloat(), returns float value
nextDouble(), returns double value
nextBoolean(), returns boolena value
[CS202 / Computer Programming 2]
3
[Getting Input]

nextByte()
Method calls to get input from the user and will return a byte value. For example:

Assume that the user input number 5, the output will be:

nextShort()
Method calls to get input from the user and will return a short value. For example:

Assume that the user input number 260, the output will be:

Course Module
nextInt()
Method calls to get input from the user and will return a int value. For example:

Assume that the user input number 25566465, the output will be:

nextLong()
Method calls to get input from the user and will return a long value. For example:

Assume that the user input number 123456789012345, the output will be:

nextFloat()
Method calls to get input from the user and will return a float value. For example:
[CS202 / Computer Programming 2]
5
[Getting Input]

Assume that the user input number 3.1416, the output will be:

nextDouble()
Method calls to get input from the user and will return a double value. For example:

Assume that the user input number 123456.78901, the output will be:

nextBoolean()
Method calls to get input from the user and will return true or false value. For
example:

Assume that the user input False, the output will be:

Course Module
Using JOptionPane Class
Another way to read input from the user is through a graphical user interface using
the JOptionPane class which is found in the javax.swing package. JOptionPane allows you
to show pop up dialog box that prompts users for a value or just show the message.
Heres the implementation of JOptionPane in a program:

The output of the progran above is:

The figure above is an input dialog box that was prompted after we run the program,
the input dialog box allows the user to enter any value.

We had input John in the input dialog box and click OK button, then the message
dialog box show up.

Let us discuss each line of code, the first statement,

tells the compiler that we want to import the JOptionPane class from the javax.swing
package.
[CS202 / Computer Programming 2]
7
[Getting Input]

Line 2,

displays JOptionPane input dialog box that is composed of a message, a text field, OK and
Cancel Button. The showInputDialog() returns String value and saved in the variable
name.
This statement,

declares msg variable with a value of Hello together with the value of variable name
and !.
The next line,

displays a message dialog box which contains a message and an OK button, in this case,
message is based from the value of msg variable.

Course Module

You might also like