Results 1 to 5 of 5
  1. #1
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default 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);
    	}
    Note: Using another word instead of this block of code has the program run just fine.

    All help is appreciated!
    Last edited by pbrockway2; 11-26-2011 at 10:48 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

  3. #3
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default 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!

  4. #4
    Join Date
    Nov 2011
    Location
    Northern Virginia, USA
    Posts
    9
    Rep Power
    0

    Post 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!

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: BufferedReader error (Stanford tutorial)

    thank you so much
    You're welcome.

Similar Threads

  1. BufferedReader throwing strange error
    By DragonGhola in forum Advanced Java
    Replies: 4
    Last Post: 06-16-2011, 11:17 AM
  2. BufferedReader & PrintWriter error
    By robertmacedonia in forum Networking
    Replies: 0
    Last Post: 09-16-2010, 12:57 AM
  3. Replies: 1
    Last Post: 05-06-2010, 11:06 PM
  4. BufferedReader error?
    By Umogrim in forum New To Java
    Replies: 2
    Last Post: 04-27-2010, 08:58 PM
  5. BufferedReader error cannot resolve symbol
    By SwEeTAcTioN in forum New To Java
    Replies: 12
    Last Post: 10-22-2009, 06:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •