Back to Java Users Group Home Print this page

Java Users Group

August 2009 Meeting Information

August 11, 2009
Special Project: java.util.Scanner

For this year's special project, Barry Burd has offered the following suggestion:

Java 1.5 introduced the java.util.Scanner class for reading tokens from a string, a file, or an input stream. The class's easy-to-use methods were convenient for writing about text-only programs in "Java For Dummies." To get an int value from the keyboard, execute myScanner.nextInt(); to get a double value, execute myScanner.nextDouble(); to get a line of text from the keyboard, execute myScanner.nextLine(); and so on. Almost every primitive type has its own nextXXX() and hasNextXXX() methods. The one exception is Java's char type. The Scanner class doesn't have nextChar() or hasNextChar() methods.

My goal in "Java For Dummies" is to make the code as readable as possible. If an example reads a single character (as in "Do you want to continue [y/n]?"), I'd like the code to contain a simple nextChar() call instead of a complicated workaround (such as myScanner.findWithinHorizon(".",0).charAt(0)). I can't afford to have my readers become sidetracked by unintuitive solutions. I'm trying to write nextChar() and hasNextChar() methods and submit the code for inclusion in Java 7. Surprisingly, this brings up all kinds of subtle issues, like trying to subclass the final Scanner class, trying to use methods in the proprietary sun.xxx.xxx packages, and writing a solution that's robust and efficient. (Remember, if I submit this to the Java Community Process, I don't want to be embarrassed.) In this session, Barry will present the problem that I'm trying to solve, and get your input on alternative solutions.