Else

You may have noticed a minor cosmetic bug in the previous program. A cosmetic bug is one that doesn’t crash the program or system, or produce incorrect results, but just looks a little annoying. Cosmetic bugs are acceptable in quick hacks you’ll only use once but not in finished code.

The cosmetic bug here was that if we didn’t include any command line arguments, although the program didn’t crash, it still didn’t say Hello. The problem was that we only used System.out.print and not System.out.println. There was never any end of line character. It was like we typed in what we wanted to say, but never hit the return key.

We could fix this by putting a System.out.println(""); line at the end of the main method, but then we’d have one too many end-of-lines if the user did type in a name. We could add an additional if statement like so:

// This is the Hello program in Java
class Hello {

    public static void main (String args[]) {

      /* Now let's say hello */
      System.out.print("Hello ");
      if (args.length > 0) {
        System.out.println(args[0]);
      }
      if (args.length <= 0) {
        System.out.println("whoever you are");
      }
  }

}

This corrects the bug, but the code is hard to read and maintain. It’s very easy to miss a possible case. For instance we might well have tested to see if args.length were less than zero and left out the more important case that args.length equals zero. What we need is an else statement that will catch any result other than the one we hope for, and luckily Java provides exactly that. Here’s the right solution:

// This is the Hello program in Java
class Hello {

    public static void main (String args[]) {

      /* Now let's say hello */
      System.out.print("Hello ");
      if (args.length > 0) {
        System.out.println(args[0]);
      }
      else {
        System.out.println("whoever you are");
      }
  }

}

Now that Hello at least doesn’t crash with an ArrayIndexOutOfBoundsException we’re still not done. java Hello works and Java Hello Rusty works, but if we type java Hello Elliotte Rusty Harold, Java still only prints

Hello Elliotte. Let’s fix that.

We’re not just limited to two cases though. We can combine an else and an if to make an else if and use this to test a whole range of mutually exclusive possibilities. For instance here’s a version of the Hello program that handles up to four names on the command line:

// This is the Hello program in Java
class Hello {

    public static void main (String args[]) {

      /* Now let's say hello */
      System.out.print("Hello ");
      if (args.length == 0) {
        System.out.print("whoever you are");
      }
      else if (args.length == 1) {
        System.out.println(args[0]);
      }
      else if (args.length == 2) {
        System.out.print(args[0]);
        System.out.print(" ");
        System.out.print(args[1]);
      }
      else if (args.length == 3) {
        System.out.print(args[0]);
        System.out.print(" ");
        System.out.print(args[1]);
        System.out.print(" ");
        System.out.print(args[2]);
      }
      else if (args.length == 4) {
        System.out.print(args[0]);
        System.out.print(" ");
        System.out.print(args[1]);
        System.out.print(" ");
        System.out.print(args[2]);
        System.out.print(" ");
        System.out.print(args[3]);
      }
      else {
        System.out.print(args[0]);
        System.out.print(" ");
        System.out.print(args[1]);
        System.out.print(" ");
        System.out.print(args[2]);
        System.out.print(" ");
        System.out.print(args[3]);
        System.out.print(" and all the rest!");
       }
      System.out.println();
  }

}

You can see that this gets mighty complicated mighty quickly. Once again no experienced Java programmer would write code like this. One of the things that makes this solution so unwieldy is that I’ve used a different print statement for every single variable. However Java makes it very easy to print multiple items at once. Instead of including just one thing in the print method’s arguments we put multiple items in there separated by + signs. These items can include variables like args[0] and constant strings like " and all the rest!". For example the last else block could have been written as

else {
  System.out.print(args[0] + " " + args[1] + " " + args[2] + " " + args[3] + " and all the rest!");
}

This syntax is simpler to read and write but would still be unwieldy once the number of command line arguments grew past ten or so. In the next section we’ll see how to handle over two billion command line arguments in a much simpler fashion.

Exercises
  1. Rework the entire program to use no more than one print method in each block.
  2. A truly elegant solution to this problem relies on statements that haven’t been introduced yet, notably for. However there is a more elegant and space efficient solution that accomplishes everything we did above, does not use the + operator, and uses only if’s and a single else. No else if’s are needed. Can you find it?

Comments are closed.