Bufferedreader Errors

时间:2016-04-15 15:06:48

标签: java exception bufferedreader

I have been trying to create a basic app for a quiz as part of my research, but I am running into trouble with the buffered reader. I have searched everywhere for info on the errors and the actual bufferedreader method, but can't find a thing. Here is my method so far, and the errors are:

unreported exception java.io.IOException; must be caught or declared to be thrown

and the other one is

missing return statement.

public String showMathsNotes() throws IOException{
    BufferedReader br = new BufferedReader(new FileReader("ReligionNotes.txt"));
    String note = br.readLine();
}

The first error comes from another method which is calling this one. Here is an extract from it:

switch(choice2){
    case 1: System.out.println(showMathsNotes());break;
    case 2: System.out.println(showEnglishNotes());break;
    case 3: System.out.println(showReligionNotes());break;
    default: System.out.println("Invalid Entry");break;

*************************EDITED***************************

I am now receiving the error

unreported exception java.io.IOException; must be caught or declared to be thrown

I have arranged the code to this now:

    public void showMathsNotes()throws IOException{
    try{
        BufferedReader br = new BufferedReader(new FileReader("MathsNotes.txt"));
        String note = br.readLine();
    }catch(IOException e){
        e.printStackTrace();
    }
}

3 个答案:

答案 0 :(得分:1)

From the documentation: An IOException is thrown by the constructor of FileReader "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."

Java forces you to do something if such an exception (error) occurs. This is done by catching the exception with a try catch clause:

try {
    // Exception may be thrown in this block
} catch(Exception e) {
    // Do something here if an exception was thrown
}

The second error you're getting is simply because you don't return any value from a method that you have marked to return a String:

public String someMethod() { // marked to return a String
    return "someString"; // something like this is needed
}

Note that you can also return a String variable, rather than a literal:

public String someMethod() {
    String str = "someString";
    return str;
}

答案 1 :(得分:0)

Your method must return a String (this is the cause of your second error, telling you that you don't return what the method should return):

public String showMathsNotes() throws IOException{
    BufferedReader br = new BufferedReader(new FileReader("ReligionNotes.txt"));
    String note = br.readLine();
    return note; // or whatever String you need to return
    }

For the other error, the calling method must catch the IOException (this is a checked Exception), or declare that it throws it :

  void callingMethod(){

  try{
      String result = showMathsNotes();
     }
     catch(IOException e){
      e.printStacktrace(); // or deal with it as you want
     }
    }

Or

void callingMethod() throws IOException{

String result = showMathsNotes();

}

答案 2 :(得分:0)

Your Method showMathsNotes can throw an Exception which has to be caught in the method which calls showMathsNotes or forwarded to the method which calls the method which calls showMathsNotes. Thats the reason for the first one.

You get the second one because showMathsNotes has to return a String because you declared it as public String showMathNotes(). Or you declare it as public void showMathsNotes() which means the method doesn't return anything. That depends on what you want.