Using Scanner to stop reading input on a blank line
Using the new java 1.5 Scanner class is tricky if you want it to stop reading input when it reaches a blank line. To do this, use two nested java.util.Scanner instances, one to read lines, the other to parse each line:
import java.util.*;
public class Test{
public static void main(String [] args){
Scanner s = new Scanner(System.in);
while(s.hasNextLine()){
Scanner ss = new Scanner(s.nextLine());
if(!ss.hasNext())
break;
// line empty, quit
while(ss.hasNext()){
// handle input types
ss.next();
}
ss.close();
}
System.err.println("Done program");
s.close();
}
}
That’s all there is to a complicated use of Scanner for line-by-line text tokenization!
Eclipse never ceases to amaze me
I’m writing documentation for my compiler due later today, and I double click on it in Eclipse:

The Eclipse IDE uses OLE to dynamically include an instance of MS Word in its project space. That is cool… I am impressed!
Enumerated Constants in Java
Java 1.5 introduces this great feature long present in other languages called an enumerated constant. In theory, it lets you replace ugly java code like this:
public static final int PANCAKE = 0; public static final int ORANGES = 0; public static final int CEREAL = 0; public static final int MILK = 0;
With a simple one line definition:
public enum breakfast { pancake, oranges, cereal, milk };
This is good because it improves the readability and maintability of the code. Enums are also strictly more powerful than a list of integers, as they are actually a shorthand for a class, and contain methods and fields of their own. However, transitioning to using java 1.5 is not as easy as you think. I wanted to use an enum in a switch / case statement, but was having trouble. My first incorrect try was to fully qualify the enum:
switch(visited.operator){
case BinaryExpression.Operator.and: ... break;
}
This gives the following error:
The enum constant BinaryExpression.Operator.and reference cannot be qualified in a case label
Searching for tutorials online didn’t help–the only one I could find that was truly useful was this one from InformIT. I finally discovered that it inherits the context from the type of the object in the switch() statement, and that cases can simply use unqualified names. The code now becomes:
switch (visited.operator) {
case and: ... break;
}
One you get used to them, enums in Java 1.5 are great. They really should have been there all along!