tags:

views:

115

answers:

5
public void populateNotesFromFile()
{
    try{
        BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED));
        String fileNotes = reader.readLine();

        while(fileNotes != null){
            notes.add(fileNotes);
            fileNotes = reader.readLine();
        }
        reader.close();
    }
    catch (IOException e){
        System.err.println("The desired file " + DEFAULT_NOTES_SAVED + " has problems being read from");
    }
    catch (FileNotFoundException e){
        System.err.println("Unable to open " + DEFAULT_NOTES_SAVED);
    }

    //make sure we have one note
    if (notes.size() == 0){
        notes.add("There are no notes stored in your note book");
    }       
}

Whenever i compile the above i get a message saying cannot find symbol class IOException e

can someone tell me how to fix it please :d

thanks

+1  A: 

You need

import java.io;

at the top of your file.

Also, FileNotFoundException needs to be above IOException since it's a subclass of IOException.

TheSoftwareJedi
java.io.*, shurely ?
Brian Agnew
A: 

You need to either import the java.io.* package at the top of the file, or fully-qualify the exception as java.io.IOException

thecoop
+3  A: 

IOException is a class from the java.io package, so in order to use it, you should add an import declaration to your code. import java.io.*; (at the very top of the java file, between the package name and your class declaration)

FileNotFoundException is a IOException. It's a specialisation of IOException. Once you have caught the IOException, the flow of the program will never get to the point of checking for a more specific IOException. Just swap these two, to test for the more specific case first (FileNotFound) and then handle (catch) any other possible IOExceptions.

Peter Perháč
yes this is correct :Dsorry i didnt see your second part to the answer originally, you must have edited your answer?!
Imports are somewhat tedious work after once you have mastered them. So it's best to configure your IDE (if it supports it) to do an authomatic insertion of import declarations to avoid problems like this.
Azder
In Eclipse, for example, I press Ctrl+Shift+O from time to time, to organise the imports. Also when a class name like IOException gets underlined (obviously a missing import), then I place my cursor into the underlined word and press Ctrl+1 for tips on resolving the error. In case the class not imported is Date, you would get a choice between java.sql.Date and java.util.Date, etc... Ctrl+1 is a really useful Eclipse shortcut.
Peter Perháč
@compsciundergrad Editing the answer is the proper way to answer an additional question. In fact, you editing your question to include the second part would have been better than a comment - as long as it's directly related.
TheSoftwareJedi
i dont have the benefits of these more professional IDE's yet, im still learning with Blue J :DThanks for all the help guys
You will most certainly be forced to resort to more powerful tool like Eclipse next year. You might as well start looking at it now. It really is a great IDE. I remember BlueJ giving me a lot of headache when all I wanted to do is write simple Java programs for coursework. The switch from BlueJ to Eclipse wasn't that smooth, I experimented with NetBeans for a while but then I settled with Eclipse. It's just very convenient and widely used and OMG the endless sea of funky plugins to spice up the IDE!
Peter Perháč
yes i did have a play around with netbeans myself actually, however i did get slightly confused and felt a little "out my depth" however if Eclipse is as good as you make it sound maybe i shall have a play around with that, and yes you are correct next year we have to move away from blue j :p
A: 

Switch the order of FileNotFoundException and IOException

Stephen Denne
yes i did this and it worked :D can you tell me why this is the case please as this is all practise i am doing at the moment and i have an exam in a week and i didn't realise order mattered
MasterPeter has explained it well enough.
Stephen Denne
+1  A: 

Your probably missing an import reference to IOException class. I'ts located in the java.io package.

Can I suggest a litle change in you method? Always close the stream in a finally block:

public void populateNotesFromFile() {
 BufferedReader reader = null;
 try {
  reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED));
  String fileNotes = reader.readLine();
  while (fileNotes != null) {
   notes.add(fileNotes);
   fileNotes = reader.readLine();
  }
 } catch (FileNotFoundException e) {
  System.err.println("Unable to open " + DEFAULT_NOTES_SAVED);
 } catch (IOException e) {
  System.err.println("The desired file " + DEFAULT_NOTES_SAVED
    + " has problems being read from");
 } finally {
  try {
   reader.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 // make sure we have one note
 if (notes.size() == 0) {
  notes.add("There are no notes stored in your note book");
 }
}
bruno conde