5 Replies - 10693 Views - Last Post: 27 February 2013 - 11:46 AM Rate Topic: -----

#1 chase552  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

Converting CSV File into 2D Array Java

Posted 26 February 2013 - 07:35 PM

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Converting CSV File into 2D Array Java

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Converting CSV File into 2D Array Java

Posted 26 February 2013 - 11:03 PM

The split method returns a String array
Remember that Strings are immutable, therefore a call to split will never change the original String
String[] row = dataRow.split(",");
list.add(row);


Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: Converting CSV File into 2D Array Java

Posted 27 February 2013 - 04:37 AM

Quote

therefore a call to split will never change the original String
That is of course true, but even if it could, i'm wondering how it would do so such that the data points could be retrieved? The point of course is that the resulting String[] must be added to the list.

Quote

Each row and column is supposed to have 25 elements each.
That's not what you mean is it?
I ask, because further on there's confusion too:

Quote

int array2d[][] = new int[10][3];

You create an array capable of holding 30 elements and then proceed to treat it as if it could hold 625. Hint: don't use magic numbers in code - they're error-prone and difficult to maintain

If your code relies on a set number of data points (anything else being invalid) then you might as well use Scanner.nextInt to fill int[][] at the outset

This post has been edited by g00se: 27 February 2013 - 04:38 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Converting CSV File into 2D Array Java

Posted 27 February 2013 - 09:07 AM

Remember that an array of size 25 goes from 0 to 24.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Converting CSV File into 2D Array Java

Posted 27 February 2013 - 10:01 AM

As Java arrays do not have to be rectangular you can always

String[][] list = new String[25][];

read line
list[index++] = line.split(",");



This post has been edited by pbl: 27 February 2013 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: Converting CSV File into 2D Array Java

Posted 27 February 2013 - 11:46 AM

Quote

As Java arrays do not have to be rectangular you can always...

Yes but an array that's not rectangular as a result of reading a csv file is a sign of a corrupt file ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1