Originally posted by richard gallagher:
thankyou
rg
Don't use == to
test Strings for equality; use the
String class equals() method.
Use "" round Strings, not ''. That code snippet won't compile because of the ''. You can't get two keystrokes into a char like that.
You can read a whole line with a Buffered Reader, or you can use the Scanner class to read a line. A Scanner will read the next char, int, etc., instead if you ask nicely (nextInt() etc), so lots of people use Scanner for simple text files instead of Readers.
You can split the line into a char[] array with a method of the String class (look through the API; it has an obvious name). You could use the charAt() method of String to find different chars within a String instead.
Don't use a break like that. Declare a String line, then
will take care of the reading. You need the extra pair of () to force the = operator to be executed before the != because = has a very low precedence.
Your closing the reader leaves you open to resource leaks if there is an exception.
You should use something like this which ensures the reader closes even if there is an IOException
The "finally" will make sure to execute that block of code; the != null bit makes sure you only close the reader if it was opened in the first place, and the outer try . . . catch will handle the exceptions.
If you use a Scanner, it consumes Exceptions, but you can get the Exception with its IOException() method.