Friday, April 24, 2009

201 how to read Tri data

The best mechanism I have found for reading the characters 'P', 'S', etc. in the data file is this:
  • read a string
  • examine the first character of the string with String.charAt(0), which looks at the first character stored with the string
  • then, for characters 'S', 'T', and 'R', use a switch on that character
One could also try various compare and equals methods of the String class, but life is short. And switch won't work on String objects, so one would have to suss out the really right boolean expression. I'm sticking with this.

Oh, and here's a program to demonstrate: click

If what was expected isn't what you got, then an Exception is thrown, which brings the program to a halt with (bonus!) an intelligible message of your choice. More about exceptions next week.

No comments: