-1

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 somewhere in the method (can you call it function?), but I'm not sure where - nor why. Here's the code:

public String[] OpenFile() throws IOException {

    Map<String, Double> map = new HashMap();

    FileReader fr = new FileReader("money.txt");
    BufferedReader br = new BufferedReader(fr);


    try{
        while (br.ready()){
            String str = br.readLine();
            String[] list = str.split(" ");
            System.out.println(list);               
        }
    }   catch (IOException e){
        System.err.println("Error - IOException!");
    }
}
10
  • 19
    You get the error "missing return statement" because there is missing return statement.. Really. Commented Jan 21, 2014 at 11:14
  • 1
    You should get some tutorial before posting question here! Commented Jan 21, 2014 at 11:14
  • 1
    make it void instead of String[] (before function name) or just return list Commented Jan 21, 2014 at 11:15
  • If you don't need the return, you can change public String[] OpenFile() to public void OpenFile() Commented Jan 21, 2014 at 11:15
  • Use Eclipse or any other IDE. it will add it for you. Good going. Kudos Commented Jan 21, 2014 at 11:15

3 Answers 3

5

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;
}
Sign up to request clarification or add additional context in comments.

Comments

4

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;
}

1 Comment

Ah, didn't notice that at all - silly mistake by me. Thanks for the help!
1
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; 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.