简体   繁体   中英

Converting CSV file into 2D Array Java

I am having some trouble converting a CSV file into a 2D array in java. I might be going the longest way around this but I cannot seem to figure our why I am getting an error. Each row and column is supposed to have 25 elements each. Here is my code:

BufferedReader CSVFile = new BufferedReader(new FileReader(fileName));

String dataRow = CSVFile.readLine();
// Read first line.
// The while checks to see if the data is null. If 
// it is, we've hit the end of the file. If not, 
// process the data.

while (dataRow != null) {
    dataRow.split(",");
    list.add(dataRow);
    dataRow = CSVFile.readLine();

    // Read next line of data.
}
// Close the file once all data has been read.
CSVFile.close();

String[] tokens = null;
Object[] test = list.toArray();

String[] stringArray = Arrays.copyOf(test, test.length, String[].class); //copies the object array into a String array 

//splits the elements of the array up and stores them into token array

for (int a = 0; a < test.length; a++) {
    String temp = stringArray[a];
    tokens = temp.split(",");

}

//converts these String tokens into ints

int intarray[] = new int[tokens.length];

for (int i = 0; i < tokens.length; i++) {

    intarray[i] = Integer.parseInt(tokens[i]);

}

//attempts to create a 2d array out of a single dimension array
int array2d[][] = new int[10][3];

for (int i = 0; i < 25; i++) {
    for (int j = 0; j < 25; j++) {
        array2d[i][j] = intarray[(j * 25) + i];

    }
}

I believe that the error is when the ArrayList gets copied to the first String array but I can't be sure. The file has 25 columns and 25 rows. The error I keep getting are that the array is out of bounds at index 25. Any input would be greatly appreciated. Thanks!

for (int a = 0; a < test.length; a++) {
    String temp = stringArray[a];
    tokens = temp.split(","); //< -- OLD VALUE REPLACED  WITH NEW SET OF TOKENS

}

tokens will only contain the tokens of the last string used , not all of the tokens seen so far. Thus tokens.length == 25 and accessing tokens[25] is an ArrayOutOfBounds exception.

You should make the below changes

ArrayList<String> tokens = new ArrayList<String>();
...
tokens.addAll(Arrays.asList(temp.split(","))); 

Create ArrayList from array explains how to add an array of elements to an arrayList.

By the way, doing your own CSV parsing is probably not the most efficient use of your time (unless this is homework). There's great libraries out there to handle this (opencsv, commons-lang3) that deal with things like quoting, empty tokens, configurable delimiters, etc....

Here's an example with commons-lang3:

StrTokenizer tokenizer = StrTokenizer.getCSVInstance();

while (...) {
    tokenizer.reset(dataLine);
    String tokens[] = tokenizer.getTokenArray();
    ...
}

Now you're free to concentrate on the actual logic of what you want to do with the data rather than the mundane act of parsing it.

And if you're just interested in collecting tokens as a flat list:

StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
List<String> allTokens = new ArrayList<String>();
while (...) {
    tokenizer.reset(dataLine);
    allTokens.addAll(tokenizer.getTokenList());
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM