Java 2 For Dummies

by Barry Burd

Please note: This page applies almost exclusively to the first edition of Java 2 For Dummies. If you have the second edition visit this page instead.


How to find out which printing you have
General tip
Chapter 2
Chapter 4
Chapter 12
Chapter 13
Notes for Mac users

Which printing do you have?

(This info applies to the English language edition only.)

In the book's first few pages, look for a page with all kinds of official-looking stuff (the publisher's address, the Library of Congress Control Number, the ISBN, and so on). On that page, look for a line with descending numbers on it.

10 9 8 7 6 5 4 3 2

The smallest number is the number of your printing. For instance, the line shown above is in books made during the 2nd printing.

A General Tip: Be sure to check the Bonus chapters on the CD-ROM for extra topics not covered in the paper portion of the book. The Bonus chapters appear in the Table of Contents, but topics in the Bonus chapters may not appear in the book's index. (This problem is fixed in the 2nd printing.)
The program compiles under Java 1.4, but it doesn't load and run in Internet Explorer.

There are several things you can do about this:

The mortage payment amounts in the examples in Chapter 2 are off by $1.06. That's because of an error in the payment-calculating formula. You can download the corrected code right here:

Thanks to Jeff Goldstein

In the section entitled "The increment and decrement operators" in Chapter 4, please note that the decrement operator is a double minus sign --. The decrement operator is not the single minus sign - that's used in many of this section's paragraphs. (This error is fixed in the 2nd printing.)

For instance, the end of the first full paragraph of that section should read as follows:
"The increment operators use double plus signs (++), and the decrement operator uses double minus signs (--). To see how they work ..."

In (Listing 4-7), change two occurrences of the word total to the word whatsLeft:
(This error is fixed in the 2nd printing.)

public class MakeChange
   public static void main(String args[])
      int quarters, dimes, nickels, cents;
      int whatsLeft, total;

      total = 248;
      quarters = total/25;
      whatsLeft = total%25;

      dimes = whatsLeft/10;
      whatsLeft = whatsLeft%10;

      nickels = whatsLeft/5;
      whatsLeft = whatsLeft%5;

      cents = whatsLeft;

      System.out.println("From " + total + " cents get");
      System.out.println(quarters + " quarters");
      System.out.println(dimes + " dimes");
      System.out.println(nickels + " nickels");
      System.out.println(cents + " cents");


When you make the change, a run of the program looks like this:

Thanks to David Wurster

In (Listing 4-8), swap the two statements that multiply numberOfBunnies by 2, and add numberExtra to numberOfBunnies:
(This error is fixed in the 2nd printing.)

public class UseAssignmentOperators
   public static void main(String args[])
      int numberOfBunnies;
      int numberExtra;

      numberOfBunnies += 1;

      numberOfBunnies += 5;

      numberOfBunnies *= 2;

      numberOfBunnies += numberExtra;

      System.out.println(numberOfBunnies -= 7);

      System.out.println(numberOfBunnies = 100);

Thanks to Bo Isaksson

The program in Listing 12-4 should have one additional statement near the end. The additional statement is highlighted in bold below:
(This error is fixed in the 2nd printing.)

import java.text.NumberFormat;

public class InventoryD
   public static void main(String args[])
      String numBoxesIn;
      int numBoxes;
      double boxPrice = 3.25;
      NumberFormat currency =

      System.out.print("How many boxes do we have? ");
      numBoxesIn = DummiesIO.getString();

         if (numBoxesIn.equals(""))
            throw new NoInputException();

         numBoxes = Integer.parseInt(numBoxesIn);
         if (numBoxes < 0)
            throw new NegativeNumberException();

         System.out.print("The value is ");

      catch (NegativeNumberException e)
         System.out.println("? That's impossible!");

      catch (NumberFormatException e)
         System.out.println("That's not a number.");

      catch (Exception e)
         System.out.print("Something went wrong, ");
         System.out.print("but I'm clueless about what ");
         System.out.println("it actually was.");
      System.out.println("That's that.");

class NegativeNumberException extends Exception

class NoInputException extends NumberFormatException
Thanks to the Java students from Toys R Us
Figure 13-2 indicates that the file is in Listing 13-3. The file is not in Listing 13-3. The file is in Listing 13-2.

Thanks to Tom Cavaiani

Macintosh Users

The most recent version of the MRJ implements an older version of Java - Java 1.1.8. So some of the code in Java 2 For Dummies won't work under the MRJ. I've created a revised version of that works with the MRJ. When you compile it, you'll get a deprecation warning, but the warning doesn't stop you from running the code.

Also, please note: To be able to read from the keyboard (as in, from Chapter 2), you have to tweak the MRJ settings. There's a picture illustrating what you have to do at

Thanks to Andrew Beckman and John Muccigrosso

Visit the book's page at Hungry Minds.

If you have any comments or questions for Barry Burd, please send email to j2fd at Thank you.