Learn Java the Hard Way (Second Edition)

Exercise 7: Getting Input from a Human

Now that we have practiced creating variables for a bit, we are going to look at the other part of interactive programs: letting the human who is running our program have a chance to type something.

Go ahead and type this up, but notice that the first line in the program is not the public class line. This time we start with an “import” statement.

Not every program needs to get interactive input from a human using the keyboard, so this is not part of the core of the Java language. Just like a Formula 1 race car does not include an air conditioner, programming languages usually have a small core and then lots of optional libraries6 that can be included if desired.

 1 import java.util.Scanner;
 3 public class ForgetfulMachine {
 4     public static void main( String[] args ) {
 5         Scanner keyboard = new Scanner(System.in);
 7         System.out.println( "What city is the capital of France?" );
 8         keyboard.next();
10         System.out.println( "What is 6 multiplied by 7?" );
11         keyboard.nextInt();
13         System.out.println( "Enter a number between 0.0 and 1.0." );
14         keyboard.nextDouble();
16         System.out.println( "Is there anything else you would like to say?" );
17         keyboard.next();
18     }
19 }

When you first run this program, it will only print the first line:

…and then it will blink the cursor at you, waiting for you to type in a word. When I ran the program, I typed the word “Paris”, but the program will work the same even if you type a different word.

Then after you type a word and press Enter, the program will continue, printing:

…and so on. Assuming you type in reasonable answers to each question, it will end up looking like this:

What You Should See

So let us talk about the code. On line 1 we have an import statement. The library we import is the scanner library java.util.Scanner (“java dot util dot Scanner”). This library contains functionality that allows us to get information into our program from the keyboard or other places like files on the computer or from the Internet.

Lines 3 and 4 are hopefully boring. On line 5 we see something else new: we create a “Scanner object” named “keyboard”. (It doesn’t have to be named “keyboard”; you could use a different word there as long as you use it everywhere in your code.) This Scanner object named keyboard contains abilities we’ll call functions or “methods”. You must create and name a Scanner object before you can use one.

On line 8 we ask the Scanner object named keyboard to do something for us. We say “Keyboard, run your next() function.” The Scanner object will pause the program and wait for the human to type something. Once the human types something and presses Enter, the Scanner object will package it into a String and allow your code to continue.

On line 11 we ask the Scanner object to execute its nextInt() function. This pauses the program, waits for the human to type something and press Enter, then packages it into an integer value (if possible) and continues.

What if the human doesn’t type an integer here? Try running the program again and type 41.9 as the answer to the second question.

The program blows up and doesn’t run any other statements because 41.9 can not be packaged into an integer value: 41.9 is a double. Eventually we will look at ways to handle error-checking for issues like this, but in the meantime, if the human types in something incorrectly which blows up our program, we will blame the human for not following directions and not worry about it.

Line 14 lets the human type in something which the Scanner object will attempt to convert into a floating-point value, and line 17 lets the human type in a String. (Anything can be packaged as a String, including numbers, so this isn’t likely to fail.)

Try running the program several more times, noticing when it blows up and when it doesn’t.



“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell