Java Read Csv File Into 2d Array

#1

  • New D.I.C Caput

Reputation: 0

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

Converting CSV File into second Array Coffee

Posted 26 February 2013 - 07:35 PM

I am having some problem converting a CSV file into a 2d array in java. I might be going the longest way around this only 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 lawmaking:

BufferedReader CSVFile = new BufferedReader(new FileReader(fileName));  String dataRow = CSVFile.readLine(); // Read showtime 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 != nada) {     dataRow.dissever(",");     list.add(dataRow);     dataRow = CSVFile.readLine();      // Read next line of data. } // Close the file once all data has been read. CSVFile.close();  String[] tokens = zilch; Object[] examination = list.toArray();  Cord[] stringArray = Arrays.copyOf(test, test.length, String[].form); //copies the object array into a Cord array   //splits the elements of the array upwardly 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 second array out of a single dimension array int array2d[][] = new int[10][three];  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 assortment but I tin't be sure. The file has 25 columns and 25 rows. The mistake I go along getting are that the array is out of bounds at index 25. Any input would be greatly appreciated. Thanks!


Is This A Adept Question/Topic? 0

  • +

#two CasiOo User is offline

Reputation: 1578

  • View blog
  • Posts: 3,551
  • Joined: 05-Apr 11

Re: Converting CSV File into 2d Array Java

Posted 26 Feb 2013 - 11:03 PM

The split method returns a String assortment
Remember that Strings are immutable, therefore a call to divide will never change the original String

Cord[] row = dataRow.split up(","); list.add together(row);            

#iii g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Converting CSV File into 2D Array Coffee

Posted 27 February 2013 - 04:37 AM

Quote

therefore a call to split will never change the original String

That is of class true, just even if it could, i'm wondering how it would do so such that the data points could be retrieved? The signal of class is that the resulting String[] must exist added to the list.

Quote

Each row and column is supposed to have 25 elements each.

That'due south non what you lot mean is it?
I ask, because further on at that place's confusion too:

Quote

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

You create an assortment capable of holding thirty elements then proceed to treat it equally if it could concur 625. Hint: don't use magic numbers in code - they're error-prone and hard to maintain

If your code relies on a prepare number of data points (anything else beingness invalid) and so y'all might as well use Scanner.nextInt to make full int[][] at the starting time

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

#4 farrell2k User is offline

Reputation: 874

  • View blog
  • Posts: 2,706
  • Joined: 29-July eleven

Re: Converting CSV File into 2D Assortment Java

Posted 27 Feb 2013 - 09:07 AM

Remember that an array of size 25 goes from 0 to 24.

#5 pbl User is offline

Reputation: 8381

  • 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 yous can always

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

This post has been edited past pbl: 27 February 2013 - ten:24 AM

#six g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Converting CSV File into second Assortment Java

Posted 27 February 2013 - 11:46 AM

Quote

As Java arrays do non have to be rectangular you tin always...

Yep but an array that's not rectangular equally a result of reading a csv file is a sign of a corrupt file ;)

havensgnitend.blogspot.com

Source: https://www.dreamincode.net/forums/topic/313664-converting-csv-file-into-2d-array-java/

0 Response to "Java Read Csv File Into 2d Array"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel