Java - Missing return statement

后端 未结 3 602
南方客
南方客 2021-01-29 10:01

I get an error - \"missing return statement\" - on line 26, which in this case is the last curly bracket in the code. I know that it literally means that I have to return somewh

3条回答
  •  野的像风
    2021-01-29 10:16

    If you just want to print the file as you are doing right now, change the return type to void as follows:

    public void OpenFile() throws IOException {
    

    Otherwise, since you are just opening and reading in a text file, you might want another String variable that you append the file's lines to and then return, as opposed to a String[]. Maybe something like this:

    public String openFile(String fileName) throws IOException {
    
        String output = "";
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);
    
        try {
            while (br.ready()) {
                String line = br.readLine();
                output += line + "\n";
            }
        } catch (IOException e) {
            System.err.println("Error - IOException!");
        }
        return output;
    }
    

提交回复
热议问题