0

I have a file to put in a multidimensional array. I have to put to [0] a date (long) and one of the dimensions must be incremented depending on the value of the second token.

Here's the code :

BufferedReader bufStatsFile  = new BufferedReader(new FileReader(statsFile));

String line = null;
List<Long[]> stats = new ArrayList<Long[]>();
stats.add(new Long[11]);
int i = 0; // will be in a loop later

while((line = bufStatsFile.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(line,";");
    while(st.hasMoreTokens()) {
        stats.get(i)[0] = Long.parseLong(st.nextToken());
        stats.get(i)[Integer.parseInt(st.nextToken())]++; // Here is the problematic line.
    }
}
bufStatsFile.close();

But the incrementation doesn't work. Maybe it is because of my array which is probably not correct, but I didn't found another proper way to do that.

2 Answers 2

1

Ok. I have found and it was, of course, stupid.

The problem was in my array declaration. I did it like that :

    List<Long[]> stats = new ArrayList<Long[]>();
    stats.add(new Long[11]);

And then, I tried to increment an Object and not a long number.

So now, I just do it like this :

    List<long[]> stats = new ArrayList<>();
    stats.add(new long[11]);

And it's perfectly working.

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

Comments

0

Check that the elements in your file are numbers from 0 to 10. Why are you having a List if you are only manipulating the row 0?

Which exception are your code throwing away?

2 Comments

Yes, the numbers in my file are numbers between 0 and 10. I have a NullPointerException. No, I have not explain very well. I have an unknown number of rows.
Try taking stats.get(i)[0] = Long.parseLong(st.nextToken()); out from the while(st.hasMoreTokens()) loop.

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.