What You Will Learn |
---|
|
The Scanner class provides two methods for reading strings from the keyboard.
The first method we will look at is the next method. The next method reads a series of characters until it encounters a delimeter then converts the characters to a string. A delimeter is a special character that separates one piece of data from another. It is usually a white space character such as a space, tab, or end-of-line character. Look at the following example:
Scanner keyboard = new Scanner(System.in);String str1 = ""; String str2 = ""; System.out.print("Enter two words --> "); str1 = keyboard.// instantiate Scanner object();nextstr2 = keyboard.// read until white space encountered();nextSystem.out.println(str1); System.out.println(str2);// read until white space encountered
Below is a sample run of the code above. The words "dog" and "cat" were input from the keyboard. There is a space between the two strings and an end-of-line marker after "cat".
Enter two words --> dog cat dog cat
The string "dog" is read by the first next method and stored in the String variable str1. The string "cat" is read by the second next method and stored in the String variable str2.
The second method used to read strings from the keyboard is the nextLine method. The nextLine method reads a series of characters from the keyboard until it encounters an end-of-line character then converts the characters to a string.
Scanner keyboard = new Scanner(System.in);String str; System.out.print("Enter a sentence --> "); str = keyboard.// instantiate Scanner object();nextLineSystem.out.println(); System.out.println(str);// reads until end-of-line marker encountered
Below is a sample run for the code above. The phrase "Give me liberty or give me death!" was entered from the keyboard.
Enter a sentence --> Give me liberty or give me death! Give me liberty or give me death!
The string "Give me liberty or give me death!" is read by the nextLine method and stored in the String variable str.
When users of a program are typing from the keyboard they press the Enter key to signify the end of data entry. The Scanner methods nextInt, nextDouble, and next will read the data from the keyboard but they do not remove the end of line marker (EOLN) left when the Enter key was pressed after entering the value or values. Look at the following illustration:
Suppose a person is prompted to type an integer value from the keyboard and they type the number 57 and press the Enter key. The 57 and an EOLN marker are then placed in a temporary memory location called the input stream buffer. The EOLN is denoted below by the symbol [EOLN].
Enter an integer --> 57
The Input Stream buffer would contain the following characters
Assume that the code segment below prompted the user to enter the value from the keyboard.
System.out.print("Enter an integer --> "); int n = keyboard.nextInt();
The nextInt method will remove the value 57 from the Input Stream buffer and store the value in the variable n, but it will not read the EOLN marker. The EOLN marker is left on the Input Stream buffer.
This senario can cause a problem in your programs when you follow a nextInt, nextDouble, or next method by a nextLine method. Let's look at an example.
Suppose that a person was prompted to type a person's age followed by their name.
Enter your age --> 16 Enter your name --> John
Here is the code segment used to read the keyboard input.
System.out.print("Enter your age -->"); int age = keyboard.nextInt();System.out.print("Enter your name -->"); String name = keyboard.nextLine();// nextIntSystem.out.println("Age = " + age); System.out.println("Name = " + name);// followed by nextLine
A sample run of the program would actually produce the following output
Enter your age -->16 Enter your name -->Age = 16 Name =
As you can see the program does not produce the desired result. The program does not allow the user to enter a name because the nextLine method read the EOLN marker left by the nextInt method. This caused the nextLine method to terminate early; not allowing the user to enter a name.
To fix this problem we are going to use what is referred to as a dummy read. Look at the modified version of the code from above.
System.out.print("Enter your age -->"); int age = keyboard.nextInt();keyboard.nextLine();System.out.print("Enter your name -->"); String name = keyboard.nextLine();// dummy read
A nextLine statement is inserted into the code between the nextInt statement and the nextLine statement. This line of code will remove the EOLN marker left by the nextInt so that the second nextLine statement will be able to read the name entered by the user.
The nextLine method of the dummy read is not included in an assignment statement. Since the rogue EOLN marker does not serve a purpose there is no need to assign it to a variable.