This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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, thenwill 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 IOExceptionThe "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.
Joined: Oct 13, 2005
On reading my last post I notice an error; there is a ) missing from the line starting "while ((line = reader.nextLine()." Sorry. Also the comment about closing the FileReader only applies of you say