Learn Java the Hard Way (Second Edition)

Exercise 2: More Printing

Okay, now that we’ve gotten that first, hard assignment out of the way, we’ll do another. The nice thing is that in this one, we still have a lot of the setup code (which will be nearly the same every time), but the ratio of set up to “useful” code is much better.

Type the following text into a single file named GasolineReceipt.java. Make sure to match what I have written exactly, including spacing, punctuation, and capitalization.

The little vertical bar (“|”) that you see on line 4 is called the “pipe” character, and you can type it using Shift + backslash (“\”). Assuming you are using a normal US keyboard, the pipe/backslash key is located between the Backspace and Enter keys.

 1 public class GasolineReceipt {
 2     public static void main( String[] args ) {
 3         System.out.println( "+------------------------+" );
 4         System.out.println( "|                        |" );
 5         System.out.println( "|      CORNER STORE      |" );
 6         System.out.println( "|                        |" );
 7         System.out.println( "| 2015-03-29  04:38PM    |" );
 8         System.out.println( "|                        |" );
 9         System.out.println( "| Gallons:       10.870  |" );
10         System.out.println( "| Price/gallon: $ 2.089  |" );
11         System.out.println( "|                        |" );
12         System.out.println( "| Fuel total:  $ 22.71   |" );
13         System.out.println( "|                        |" );
14         System.out.println( "+------------------------+" );
15     }
16 }

Notice that the first line is the same as the previous assignment, except that the name of the class is now GasolineReceipt instead of FirstProg. Also note that the name of the file you’re putting things into is GasolineReceipt.java instead of FirstProg.java. This is not a coincidence.

In Java, each file can contain only one public class, and the name of the public class must match the file name (capitalization and all) except that the file name ends in .java and the public class name does not.

So, what does “public class” mean in Java? I’ll tell you when you’re older. Seriously, trying to go into that kind of detail up front is why most “beginner” programming books are bad for actual beginners. So don’t worry about it. Just type it for now. (Unfortunately, there’s going to be a lot of that.)

You will probably notice that the second line of this program is exactly the same as the previous assignment. There are no differences whatsoever.

Then, after the second open curly brace, there are twelve printing statements. They are all identical except for what is between the quotation marks.

Once everything is typed in and saved as GasolineReceipt.java, you can compile and run it the same way you did the previous assignment. Switch to your terminal window, change the directory into the one where you are saving your code and type this to compile it:


If you are extremely good with annoying details and fortunate, you will have no errors and the javac command will complete without saying anything at all. Probably, you will have errors. If so, go back and compare what you type with what I wrote very carefully. Eventually you will discover your error(s). Fix them, save the file again, and try compiling again.

Once it compiles with no errors, you can run it like before:


And you should see output like this:

Two programs done. Nice! What you have accomplished so far is not easy, and anyone that thinks it is has a lot of experience and has forgotten what it is like to try this stuff for the first time. Don’t quit! Work a little every day and this will get easier.

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