user1719605
user1719605

Reputation: 189

Outcome of array is null?

I posted a question not long ago, which was answered and helped me a lot. Sorry to post again so soon. =/

I have gone over this, and not sure what I have done wrong, the printed outcome is several 'null' words. I am trying to populate a 2d array with data from a text file by using a method. Then run that method in the main class to print out the populated array.

Any help would be greatly appreciated, thank you in advance. =)

Main Class:

public static void main(String[] args) throws IOException {

    ScoreProcessor scores = new ScoreProcessor();

    String[][] table = scores.readFile();


    for (int row = 0; row < table.length; row++) {
        for (int col = 0; col < table[row].length; col++) {
            System.out.print(table[row][col] + "\t");
        }
    }
}

Another Class, that includes the method:

    public class ScoreProcessor {

    static public String[][] 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.hasNext()) {

            Scanner readfile = new Scanner(filedata);
            readfile.nextLine();
            readfile.useDelimiter(",");

            while (readfile.hasNext(",")) {
              String line =  readfile.next();


             line = scores[row][col] ;
                col++;
            } // end innerloop
            row++;
            col = 0;


        } // end outerloop

        return scores;
    } // end method
} // end class

Data File

Student,Q1,Q2,Q3,Q4
abcd0001,50,55,59,72
efgh0002,82,78,84,86
ijkl0003,42,45,46,52
mnop0004,66,74,72,78
qrst0005,82,86,89,88
uvwx0006,77,83,87,82
yzab0007,62,64,70,68

Upvotes: 1

Views: 121

Answers (6)

Filip
Filip

Reputation: 877

The line

while (file.hasNextInt())

will return false. Hence your array will not be initialised with data and return null prints.

I would advice to put your check on the fact if readfile.nextLine() returns null or not.

Upvotes: 2

StampedeXV
StampedeXV

Reputation: 2805

Isn't also this (besides HasNextInt()) the core of the problem:

line = scores[row][col] ;

should be other way around. This way your array will never be filled and of course always contain only nulls.

scores[row][col] = line;

Upvotes: 0

cameron
cameron

Reputation: 3006

try this:

 public class ScoreProcessor {

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

            File filedata = new File("src/JavaApp2/Data.txt");
            Scanner file = new Scanner(filedata);

            int row = 0;

            String[][] scores = new String[8][5];

            while(file.hasNextLine()){
                String line=file.nextLine();
                scores[row]=line.split(",");
                row++;
            }

            return scores;
        } 

        public static void main(String[] args) throws IOException {

            String[][] table = readFile();

            for (int row = 0; row < table.length; row++) {
                for (int col = 0; col < table[row].length; col++) {
                    System.out.print(table[row][col] + "\t");
                }
                System.out.print("\n");
            }
        }

    }

Upvotes: 1

Diego Pino
Diego Pino

Reputation: 11586

As others pointed out, file.hasNextInt() returns false, as the you're reading lines not int. Since it's null your code never gets into the loop and the data is not feeded.

   Scanner readfile = new Scanner(filedata);
   String line1 = readfile.nextLine();
   readfile = readfile.useDelimiter(",");

Don't open the file again, just read each line and split it by ','

String line = file.nextLine();

for (String each: line.split(",")) {
   scores[row][col] = each;
}

Upvotes: 2

Mukul Goel
Mukul Goel

Reputation: 8467

use file.hasNext() insted of file.hasNextInt() and it should resolve the problem

Upvotes: 0

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17435

Check out the definition of hasNextInt(). It will return false if the next token is not intepretable as an int.

The way I see it, your file starts with "Student", or if you skip the header row, with "abcd0001", but not an int. So hasNextInt() will return false right away, and your inner loop is never exeucted.

Beyond that, it's not clear to me why you instantiate a second Scanner.

An issue like this can be very easily traced with a debugger, I suggest you familiarize yourself with that approach :)

Upvotes: 0

Related Questions