1

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.

1
  • 5
    You need to give readFile as return type of String[][] I would also use your IDE to fix your formatting. Commented Oct 4, 2012 at 10:07

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

About returning declare your return type in method.

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

About printing you can use

Arrays.deepToString(scores);

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.