usert4jju7
usert4jju7

Reputation: 1813

java - method return type confusion

I'm trying to return a 2D matrix from a function. Eclipse complains about the return type of the function

import java.io.*;
import java.util.Arrays;
import java.util.List;
import com.opencsv.CSVReader;

public class myClass {

    private static String inFile = "myfile.csv";

    private String[][] readCsv() {
        File file1 = new File(inFile);
        if (file1.exists()) {
            System.out.println("File " + inFile + " exists. length : " + inFile.length());
        } else {
            System.out.println("File " + inFile + " does not exist!");
        }

        try {
            // Read all
            CSVReader csvReader = new CSVReader(new FileReader(new File(inFile)));
            List<String[]> list = csvReader.readAll();

            // Convert to 2D array
            String[][] dataArr = new String[list.size()][];
            dataArr = list.toArray(dataArr);

            return dataArr;
        } catch (Exception ex) {
            // Do something with mistake or ignore
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        myClass mc = new myClass();
        String[][] csvContents = mc.readCsv();

        for (int k = 0; k < 10; k++) {
            System.out.println(Arrays.toString(csvContents[k]));
        }
    }

}

The error eclipse tells me is This method must return a result of type String[][]. The value being returned, dataArr is of type String[][] & the method readCsv is instructed to return String[][].

I can't quite figure out where the mistake is.

Upvotes: 1

Views: 149

Answers (4)

Mureinik
Mureinik

Reputation: 311308

The method must return a String[][] in every possible branch. Here, you only return it from the try - if it doesn't complete successfully and an exception is thrown, the catch will complete without returning anything, thus the error. You could just return after the try-catch block, like @Marv suggested, but IMHO, the proper Java-way would be to just allow the exception to be thrown up out of the method instead of catching it:

// should probably throw something more specific
private String[][] readCsv() throws Exception { 
    // Read all
    CSVReader csvReader = new CSVReader(new FileReader(new File(inFile)));
    List<String[]> list = csvReader.readAll();

    // Convert to 2D array
    String[][] dataArr = new String[list.size()][];
    dataArr = list.toArray(dataArr);

    return dataArr;
}

Upvotes: 3

Or Ami
Or Ami

Reputation: 91

When an exception is caught and the code function continues, there is no return value

Upvotes: 2

Luke Joshua Park
Luke Joshua Park

Reputation: 9805

You aren't returning anything in the event that an exception is caught in readCsv. You need to return something here.

catch (Exception ex) {
        // Do something with mistake or ignore
        ex.printStackTrace();
        return SOMETHING;
    }

Upvotes: 2

Marv
Marv

Reputation: 3557

You will have to return something in case the try block throws an exception:

public String[][] myMethod() {
    String[][] arr = new String[1][];
    try {
        arr = methodThatCanThrowException(); // If this method throws an exception
        return arr;                          // this return will not execute
    } catch (Exception ignored) {}

    return arr;                              // Return something after the try/catch block
}

How you handle this is up to you, you can either return some default value or perhaps null.

Upvotes: 5

Related Questions