datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Obtaining Input

Most of our programming applications will need to interact with the user. We will need to both display output to, and obtain input from, the user. We will be using a command line interface, known as the console, to obtain input.

Many authors of introductory textbooks provide their own input routines, so students can get input easily from the keyboard. Be aware that different programmers may be using different input commands.

d
In Eclipse, we will be using the Scanner class found in the java.util package. While there are several ways to obtain input from a user, the Scanner class is considered the simplest method.  This is a relatively new class which did not exist prior to Java 1.5.0.
Using the Scanner class:

1. Import the Scanner class. This class is not in the java.lang package, so you will need to import it for the program to be able to access it. Type this line at the beginning of your program, below your initial comments.
import java.util.Scanner;

2.  Create a Scanner object. We will create an object called "reply" (or any name you like) which is a Scanner, to take input. The process of creating a new object is called instantiation.
Scanner reply = new Scanner(System.in);

3. Obtain and store the user input. There are several options of obtaining user data based upon the "type" of data desired.

a) nextInt()
If user's response will be an integer value:
The command reply.nextInt() will accept an integer value from the user as input. The "next integer" that is typed at the keyboard will be accepted as the input, and it will be stored in an integer variable you create.

System.out.print ("Enter your desired test grade: ");
int grade =
reply.nextInt();

(If the user types in something other than an integer, the program will crash. Be sure to type your instructions clearly so the user will understand exactly what is to be entered.)


b) nextDouble()
If user's response will be a double value (decimal):
The command reply.nextDouble() will accept a decimal value from the user as input. The "next double" that is typed at the keyboard will be accepted as the input, and it will be stored in a double variable you create. The option of reply.nextFloat() is also available.

System.out.print ("Enter your GPA to nearest tenth: ");
String name =
reply.nextDouble();


c) nextLine()
If user's response will be text (a String value):
The command reply.nextLine() will accept the input. The "next line" of text that is typed at the keyboard will be accepted as the input, and it will be stored in a String variable you create.

System.out.print ("Enter your full name: ");
String name =
reply.nextLine();

When using the nextLine() method of the Scanner class, the application will wait for the user to enter text with the keyboard. The entry is completed when the user presses the Enter key. The String used to store the user's input will contain all entries, including white (blank) spaces.

The method next() is also an option. It will, however, only return what comes before a space. Whereas, nextLine() returns the entire line (spaces included) up to where the user hits Enter.

d) What about characters?
 
If the user's response will be char (a single character):
There is no specific command in the Scanner class for accepting characters. To get around this problem, you can use the next( ) in combination with charAt(0) to accept a single character.

System.out.print ("Enter your middle initial: ");
char initial =
reply.next().charAt(0);


4.
Close the Scanner object.
To avoid an issue called "resource leak" in Eclipse, close the access to the Scanner class before ending your program.

reply
.close();

If you have multiple inputs from the user of data types involving numerics (int, double) and strings (String), be sure to close the scanner at the END of the program. If you close after your first use of the first data type, the underlying stream (System.in) will get closed and your future attempts to receive input of other data types will fail (actually, you will get an error). Once the stream (System.in) gets closed in a program, you will not be able to re-open it in that program.


beware Confusion arises when utilizing the Scanner class with a mixture of numeric data types (int, double) with the String data type. The problem is that the newline character of the input may be left on the line to be picked up as input on the next scan for user input. Trying to scan for a String after scanning for a numeric response is the most troublesome. To avoid the confusion, you can create a new "reply" variable for each "type" of data, as shown in the example below. Or, you can include an additional reply.nextLine(); below each input of double or int data to "eat up" the remaining newline character which is causing the problem.


It can take a while to feel comfortable with the Scanner class when working with different variable types, especially when dealing with the problem of String variables following int variable, for example. While it may not be the most efficient method, you can separate your Scanner requests (reply1, reply2, reply3) if you run into trouble. As you become more familiar with the Scanner class, you will be able to consolidate these requests and remember where the errors may occur. Check out the example below showing multiple requests, and then showing a consolidation.

Example code for input and output:

reddot Using Multiple Scanner references:

exinputpic1
exinputpic4

notebox Did you notice that a "reply3" was not created for the String dogcolor?
A scanner was already available (reply1) for use with String variables,
so creating another was not needed.

Screen Display: (notice that a user's typed responses appear in green)

inputscreen

reddot Using One Scanner reference:

dogN2
Notice that this solution used ONLY ONE request to the Scanner on reply. Before asking for the String (after having asked for a numeric), the code reply.nextLine(); was inserted to eat up the left over new line character that will prevent us from retrieving the String data. The trick is to remember "when" and "where" you need to use this line of code to prevent a problem.

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.