user1719605
user1719605

Reputation: 189

How come I cannot return the value of my array?

I'm quite new to Java, so there is probably a simple explanation to this and I'll probably feel stupid after but anyhow.

I am trying to use a method to read a file, populate a 2d array from that files data, and then return the populated array so that I can use it in my main class and from there print out the arrays contents.

This is what I have got so far:

public class ScoreProcessor { 
    static public readFile() throws IOException {
        File filedata = new File("src/JavaApp2/Data.txt");        
        Scanner file = new Scanner (filedata);        
        int row = 0, col = 0;            
        String[][] scores = new String[8][5];

        while (file.hasNextInt()){               
            Scanner readfile = new Scanner (filedata);
            readfile.nextLine();
            readfile.useDelimiter(",");

            while (readfile.hasNext(",")){
                String line = readfile.next();
                scores[row][col] = line; 
                col++;  
            }            
            row++;  
            col=0;
        }
        return scores;
    }    
}

Any help at all will be appreciated, thanks.

Upvotes: 1

Views: 105

Answers (3)

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

As Peter Lawrey rightly says you need to add the rtuerntype String[][] to your method head like so:

static public String[][] readFile() throws IOException {
   ...

Also, you should not use Arrays if you don't know the size in advance. Use lists in that case.

Upvotes: 2

Amit Deshpande
Amit Deshpande

Reputation: 19185

About returning declare your return type in method.

 public static String[][] readFile() throws IOException {

About printing you can use

Arrays.deepToString(scores);

Upvotes: 0

bigGuy
bigGuy

Reputation: 1744

static public String[][] readFile() throws IOException {

Upvotes: 1

Related Questions