Objective #1: Read program input using the Scanner class.
- While you will not be tested on how to read
input on the AP exam, you will be given questions
where the test authors provide you with a method that gets input. For example, you might be told to
assume there is a static method named readInt of
the fictitious IO class
that allows the user to input an integer and then given this code
int num = IO.readInt();
- The information below on the Scanner class will not be covered on class tests or the AP Exam. However, AP exam questions will use Scanner but you won't be asked specific Scanner-related questions.
- You can use the Scanner class to obtain user input however. To use the Scanner class, you must import it at the top of your Java source file with the statement
- To instantiate a Scanner object, you can use the following statement
Scanner keyboard = new Scanner(System.in);
In this statement, keyboard could be any variable name of your choice but it emphasizes that this statement connects the Scanner object with the keyboard which is known as System.in.
- The next method of the Scanner class can be used to read a String value as in
String word = keyboard.next();
The next method returns the String value consisting of the next keyboard characters up to, but not including, the first delimiter character. The default delimiter character is a blank space or new line character (\n). The delimiter character is left in the keyboard buffer. This can cause potential problems if it is followed by a call to nextLine since the nextLine method will read the \n as the next line. So you should probably include an extra call to nextLine after any use of next.
- The nextInt method can be used to read a numerical value and store it in an int or double variable as in
int num = keyboard.nextInt();
int num = keyboard.nextDouble();
Any new line character that is entered after the user's numerical value is left in the buffer however. This can cause problems if the nextLine method is used immediately after. So you should always include an extra call to nextLine after any use of nextInt or nextDouble.
- The nextLine method is used to read a line of text that can include spaces as in
String sentence = keyboard.nextLine();
But be careful. If you previously inputted data with a recent call of next, nextInt or nextDouble, you will need an extra call to nextLine to
get rid of the new line character that's still "hanging out" in the buffer. This is called "clearing" or "flushing" the buffer. The nextInt and nextDouble methods
consume (i.e. overlook) leading whitespace but not trailing whitespace while nextLine does the opposite.
- The nextBoolean method is used to read a true or false value from the keyboard. Any combination of upper and lowercase letters may be used in the spelling of "true" and "false".
- View the following code segment that illustrates a few Scanner methods
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = keyboard.nextLine(); // using next() would not input a name that includes spaces
System.out.println("Hello " + name);
System.out.print("Enter your test scores separated by spaces: ");
int test1 = keyboard.nextInt();
int test2 = keyboard.nextInt();
int test3 = keyboard.nextInt();
int sum = test1 + test2 + test3;
System.out.println("Your total test score sum is " + sum);
- Another way to instantiate a Scanner object would be to use the following statement
Scanner stringInput = new Scanner("1 2 3 4");
which allows you to "pick off" these integers one at a time and then use them as input in your program.
- More explanation on the Scanner class can be found at http://techcenter.davidson.k12.nc.us/scanner.swf and, of course, in the Java API.
Objective #2: Read input from an external file or web page.
Objective #3: Obtain input from the user with the showInputDialog method.
- The information below on JOptionPane is not covered on class tests or the AP Exam.
- To read input through a dialog box similar to an InputBox's
in Visual Basic, you can use the showInputDialog static
method of the JOptionPane class.
String userName = JOptionPane.showInputDialog("Enter your name:");
allows the user to input his name. If you would like the user to input an integer,
you can use the statement
int num = Integer.parseInt(JOptionPane.showInputDialog("Enter your age:"));
In order to use the JOptionPane class in a program, you must add import javax.swing.JOptionPane; to the top of your program.
In any program that uses showInputDialog method,
you must add the statement
to the end of your program in order to force the extra thread created by the
graphical user interface to close.
Here's a simple demo program:
public class Junk
public static void main(String args)
String name = JOptionPane.showInputDialog("enter your name");
JOptionPane.showMessageDialog(null, "Goodbye " + name);