Learn Java the Hard Way (Second Edition)

Exercise 8: Storing the Human’s Responses

In the last exercise, you learned how to pause the program and allow the human to type in something. But what happened to what was typed? When you typed in the answer “Paris” for the first question, where did that answer go? Well, it was thrown away right after it was typed because we didn’t put any instructions to tell the Scanner object where to store it. So that is the topic of today’s lesson.

 
 1 import java.util.Scanner;
 2 
 3 public class RudeQuestions {
 4     public static void main( String[] args ) {
 5         String name;
 6         int age;
 7         double weight, income;
 8 
 9         Scanner keyboard = new Scanner(System.in);
10 
11         System.out.print( "Hello. What is your name? " );
12         name = keyboard.next();
13 
14         System.out.print( "Hi, " + name + "! How old are you? " );
15         age = keyboard.nextInt();
16 
17         System.out.println( "So you're " + age + ", eh? That's not very old." );
18         System.out.print( "How much do you weigh, " + name + "? " );
19         weight = keyboard.nextDouble();
20 
21         System.out.println( weight + "! Better keep that quiet!!" );
22         System.out.print("Finally, what's your income, " + name + "? " );
23         income = keyboard.nextDouble();
24 
25         System.out.print( "Hopefully that is " + income + " per hour" );
26         System.out.println( " and not per year!" );
27         System.out.print( "Well, thanks for answering my rude questions, " );
28         System.out.println( name + "." );
29     }
30 }

Just like the last exercise, when you first run this your program will only display the first question and then pause, waiting for a response:

Notice that the first printing statement on line 11 is print() rather than println(). Because of this, the question is printed on the screen, but the cursor is left alone after printing. Thus the cursor is left blinking at the end of the line the question is on.

If you had used println(), the cursor would have been moved to the next line before the Scanner’s next() method had a chance to run, and so the cursor would be blinking on the beginning of the line after the question instead.

What You Should See

At the top of the program we declared four variables: one String variable called name, one integer variable called age, and two floating-point variables named weight and income.

On line 12 we see the keyboard.next() that we know from the previous exercise will pause the program and let the human type in something it will package up in a String. So now where does the String they type go? In this case, we are storing that value into the String variable named “name”. The String value gets stored into a String variable. Nice.

So, assuming you type Brick for your name, the String value "Brick" gets stored into the variable name on line 12. This means that on line 14, we can display that value on the screen! That’s pretty cool, if you ask me.

On line 15 we ask the Scanner object to let the human type in something which it will try to format as an integer, and then that value will be stored into the integer variable named age. We display that value on the screen on line 17.

Line 19 reads in a double value and stores it into weight, and line 23 reads in another double value and stores it into income.

This is a really powerful thing. With some variables and with the help of the Scanner object, we can now let the human type in information, and we can remember it in a variable to use later in the program!

Before I wrap up, notice for example that the variable income is declared all the way up on line 7 (we choose its name and type), but it is undefined (it doesn’t have a value) until line 23. On line 23 income is finally initialized (given its first value of the program). If you had attempted to print the value of income on the screen prior to line 23, the program would not have compiled.

Anyway, play with typing in different answers to the questions and see if you can get the program to blow up after each question.

More on Declaring Variables

Java is a statically-typed programming language. That means that the type of a variable (int, double, String, etc) is static: it can’t be changed. Almost all older programming languages are statically-typed, and most languages with static typing also have what is called “manifest typing”; these languages make you declare what types your variables are going to be before you can use them. Java, C, and C# are all languages with manifest typing.

Once you’ve told the Java compiler that, say, age is going to hold an integer, it makes sure that you don’t change your mind later and try to put a String in there.

This might seem a bit annoying, but when you start writing much longer programs, the type checker can help prevent certain kinds of errors. And that’s nice. But don’t feel too sorry for yourself! One of the oldest programming languages ever (Plankalkül) wasn’t so nice. You had to write the type of a variable every time you used it. This would be very annoying. I have written some code to show what Java might be like if you had to add the type of a variable every time you used it. Don’t bother typing it up; it won’t compile since Java (thankfully) doesn’t work like that.

RudeQuestionsTypesEverywhere.java
 1 public class RudeQuestionsTypesEverywhere {
 2     public static void main( String[] args ) {
 3         keyboard[Scanner] = new Scanner(System.in);
 4 
 5         System.out.print( "Hello. What is your name? " );
 6         name[String] = keyboard[Scanner].next();
 7 
 8         System.out.print( "Hi, " + name[String] + "! How old are you? " );
 9         age[int] = keyboard[Scanner].nextInt();
10 
11         System.out.println("So you're " + age[int] + ", eh? That's not so old.");
12         System.out.print( "How much do you weigh, " + name[String] + "? " );
13         weight[double] = keyboard[Scanner].nextDouble();
14 
15         System.out.println( weight[double] + "! Better keep that quiet!!" );
16         System.out.print("Finally, what's your income, " + name[String] + "? " );
17         income[double] = keyboard[Scanner].nextDouble();
18 
19         System.out.print( "Hopefully that is " + income[double] + " per hour" );
20         System.out.println( " and not per year!" );
21         System.out.print( "Well, thanks for answering my rude questions, " );
22         System.out.println( name[String] + "." );
23     }
24 }

Whew! Aren’t you glad you don’t have to do that every time you use a variable?!?

On the other hand, you are allowed to feel a little bit sorry for yourself that you even have to declare the types of variables at all. Many modern programming languages feature “type inference”, where the compiler can infer the type of a variable by how you use it in the code. Python and Swift are some programming languages that can infer types. Java does not have type inference, but I have written up some code that shows what this exercise might look like if it did. Again, don’t bother typing this one up; it won’t compile.

RudeQuestionsImpliedTypes.java
 1 public class RudeQuestionsImpliedTypes {
 2     public static void main( String[] args ) {
 3         keyboard = new Scanner(System.in);
 4 
 5         System.out.print( "Hello. What is your name? " );
 6         name = keyboard.next();
 7 
 8         System.out.print( "Hi, " + name + "! How old are you? " );
 9         age = keyboard.nextInt();
10 
11         System.out.println( "So you're " + age + ", eh? That's not very old." );
12         System.out.print( "How much do you weigh, " + name + "? " );
13         weight = keyboard.nextDouble();
14 
15         System.out.println( weight + "! Better keep that quiet!!" );
16         System.out.print("Finally, what's your income, " + name + "? " );
17         income = keyboard.nextDouble();
18 
19         System.out.print( "Hopefully that is " + income + " per hour" );
20         System.out.println( " and not per year!" );
21         System.out.print( "Well, thanks for answering my rude questions, " );
22         System.out.println( name + "." );
23     }
24 }

For example, on line 6 a compiler for such a language would be able to infer that name is a variable and that it must for holding Strings since that’s what you’re putting into it.

Some programming languages do one step further and allow you to change the type of a variable from one part of the code to another. These languages are called “dynamically typed”. Ruby, Javascript and Perl are popular dynamically-typed languages. Dynamic typing makes them more powerful, but it also makes certain types of errors possible that would be easily caught by a static type checker.

Not that any of that matters, because you’re learning Java right now and not any of those other fine programming languages. So you’re stuck with static typing and manifest typing and you’ll get used to it soon enough. Is it too much to ask to just specify the type of a variable before you start putting values in there? I think you’ll find it isn’t too bad.

Maybe you should scroll back up to the top to read the code again for this exercise before attempting the Study Drills, if you didn’t do them already.


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