Results 1 to 5 of 5
- 11-26-2011, 10:42 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 21
- Rep Power
- 0
BufferedReader error (Stanford tutorial)
Hey I'm new to the forums, and following the Stanford tutorial, I'm having trouble getting my hangman program to work because of this line: "BufferedReader rd = new BufferedReader(new FileReader("HangmanLexicon.txt"));". It's the only line underscored red in the program.
("new FileReader("HangmanLexicon.txt")" Appears underscored red with the error message "Unhandled exception type FileNotFoundException"
Here's a block of code for the method I'm trying to do.
Java Code:// Generates word list from a .txt file and selects a word randomly private void getWord() { int size = 0; BufferedReader rd = new BufferedReader(new FileReader("HangmanLexicon.txt")); ArrayList<String> sList = new ArrayList<String>(); try { while (true) { String line = rd.readLine(); if (line == null) break; sList.add(line); } rd.close(); } catch (IOException ex) { throw new ErrorException(ex); } int wordNum = (rgen.nextInt(1, sList.size())) - 1; word = sList.get(wordNum); }
All help is appreciated!Last edited by pbrockway2; 11-26-2011 at 10:48 AM. Reason: code tags added
- 11-26-2011, 10:58 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Re: BufferedReader error (Stanford tutorial)
Hi Jossos, welcome to the forum!
When you post code it is a good idea to use the "code" tags. You put [code] at the start of the code and [/code] at the end. That way the code is formatted properly and is readable when it appears here.
-----
The problem is with the FileReader constructor. As you can see from its api docs it "Throws: FileNotFoundException - if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading." And you have to deal with this exception, just as you dealt with the IOException that can be thrown by readLine().
One way of doing this would be by putting the line that declares and initialises rd inside the try block. This has the virtue of declaring the variable close to where it is used (ie not in a broader scope) - something that is often regarded as good style.
You have to catch the exception, of course. (which is what the compiler is really telling you). And you might want to read up on the FileNotFoundException as you decide how to go about catching it.
- 11-26-2011, 11:13 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 21
- Rep Power
- 0
Re: BufferedReader error (Stanford tutorial)
OMG thank you so much! I can't believe I put this off for 3 months when I could have just come here to find out! What an awesome forum this is!
- 11-26-2011, 01:52 PM #4
Re: BufferedReader error (Stanford tutorial)
Nothing beats a good forum!
Are you using an IDE? The poll here suggests most people use Eclipse with the second most popular choice being NetBeans. I like NetBeans and one reason I like it is that, whenever I forget to put a try/catch block around anything that can throw an exception, NetBeans marks the line that needs it and, if I press Alt-Enter, it offers to add the necessary lines of code for me. I don't know if Eclipse does something similar, but I infer you are using some kind of IDE (since you mention your problematic line is underlined in red), so maybe a feature like I'm describing is available in whatever you're using. (If it's not, give NetBeans a try!)
Have fun!
- 11-27-2011, 01:01 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Similar Threads
-
BufferedReader throwing strange error
By DragonGhola in forum Advanced JavaReplies: 4Last Post: 06-16-2011, 11:17 AM -
BufferedReader & PrintWriter error
By robertmacedonia in forum NetworkingReplies: 0Last Post: 09-16-2010, 12:57 AM -
in = new BufferedReader( new FileReader(storageFile)) ; // error
By nurkhasanah in forum AWT / SwingReplies: 1Last Post: 05-06-2010, 11:06 PM -
BufferedReader error?
By Umogrim in forum New To JavaReplies: 2Last Post: 04-27-2010, 08:58 PM -
BufferedReader error cannot resolve symbol
By SwEeTAcTioN in forum New To JavaReplies: 12Last Post: 10-22-2009, 06:22 AM
Bookmarks