Java - Missing return statement

后端 未结 3 600
南方客
南方客 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:07

    Ok first of all please note that this is not really a beginners forum for learning any programming language. You will find it very hard to get a very basic question answered here without approximately 20 people pointing you in the direction of Google (myself included)

    That being said please look for java tutorials, beginners guides, or simply the docs. The answer will almost everytime jump you straight in the face since there is just so much material on the java programming language

    Stackoverflow is much more a question and answer site for more abstract problems, never-seen-before issues and tricky nuts for experts - all things that are likely to cater to a general audience and to help many people, not just yourself. Admittedly you posed your question quite nicely and formatted it well, so kudos to you and welcome to the site

    As for this very specific problem you have the following options:

    Declare a void return type

    public void openFile() throws Exception {
    
    }
    

    Return a preliminary null until you're sure what to return

    public String[] openFile() throws Exception {
        return null;
    }
    

    (But make sure that the calling entity knows what's coming back ;))

    Return some valid data to the entity that called the method

    public String[] openFile() throws Exception {
        String[] myArray = new String[5];
        return myArray;
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-29 10:25
    public String[] OpenFile() // expected return type is String array
    

    But you didn't return that from your method.

    If you don't want to return that change your method return type to void(no return)

    public void OpenFile() throws IOException {
    
    }
    

    Or if you want to return as expected use

    public String[] OpenFile() throws IOException {
    
     String[] arr=new String[5];
    
     // implementation
    
     return arr; 
    

    }

    0 讨论(0)
提交回复
热议问题