2

I am reading in a CSV file and putting each delimited element into a two-dimensional array. The code looks like this:

public DataProcess(String filename, String[][] contents, int n) {//n is 6 for contents, 5 for fiveMinContents
        Scanner fileReader = null;
        try {
            fileReader = new Scanner(new File(filename));
        } catch (FileNotFoundException ex) {
            System.out.println(ex + " FILE NOT FOUND ");
        }
        fileReader.useDelimiter(",");
        int rowIndex = 0;
        while (fileReader.hasNext()) { 
            for (int j = 0; j < n; j++) {
                contents[rowIndex][j] = fileReader.next();
                 System.out.println("At (" + rowIndex +", "+j+"): " +
                 contents[rowIndex][j]);
            }
            rowIndex++;
            fileReader.nextLine();
        }
    }

I am not sure why it reads every other line of this particular CSV file because this is file 2/2 that is being read in this manner. The first one reads fine, but now this one skips every other line. Why would it work for one but not the other? I am running this on Eclipse's latest update.

I also checked out this answer and it did not help.

Community
  • 1
  • 1
Nick
  • 823
  • 2
  • 10
  • 22
  • @ReutSharabani No, the two files have almost the exact same structure. The only thing that is different is that the second file has an additional row with a number in military time Ex. 22:05. – Nick Apr 12 '16 at 05:46

3 Answers3

2

Because the last line of your loop reads a line and discards it. You need something like,

while (fileReader.hasNextLine()) { 
    String line = fileReader.nextLine();
    contents[rowIndex] = fileReader.split(",\\s*");
    System.out.println("At (" + rowIndex + "): "
            + Arrays.toString(contents[rowIndex]));
    rowIndex++;
}

You could also print the multi-dimensional array with one call like

System.out.println(Arrays.deepToString(contents));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • An example of the file is: "1/2/2005","19:05", "116.47", "116.47","116.12","116.17". If I use the code you suggested `contents[rowIndex] = fileReader.split("\\s+");` will that put the delimited values into places in the multidimensional array? I was under the impression to assign values to MD arrays, one needed to use `contents[someRow][someColumn] = someValue`. Is the way you suggested a different way of doing it? – Nick Apr 12 '16 at 05:51
  • @NickPredey Yes, in that `String.split` returns an array. A multidimensional array is an array of arrays. So `contents[someRow]` is assignable ***as*** an array. – Elliott Frisch Apr 12 '16 at 20:08
2

While the approach may work for you, it's not optimal. There are premade CSV readers for Java. One example is commons-csv:

Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
    String date = record.get(1);
    String time = record.get(2);
    // and so on, so forth
}

There are a small number of dependencies that have to be on your classpath. Hope that helps.

hd1
  • 33,938
  • 5
  • 80
  • 91
-1

I found the issue to this problem.

First, I recommend using the external library that was suggested.

The issue was that since this second file was reading the entire row, whereas the first CSV file was reading what I wanted it to, but there was a column at the end of the file that I was ignoring. There must be a way that a CSV file is structured where the end of a row has a different delimiter or something along those lines--not sure. To fix this issue, I just added an extra column to the second file and I am not reading it in; it is just there.

In short, use an external CSV-reader library. If you don't want to do that, then just add a column directly after the last column in the file and do not read it.

Nick
  • 823
  • 2
  • 10
  • 22