How to Read a Csv File Into a 2d Array

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: five
  • Joined: 09-January thirteen

Converting CSV File into 2nd Assortment Java

Posted 26 Feb 2013 - 07:35 PM

I am having some problem converting a CSV file into a second array in java. I might be going the longest way around this just 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));  Cord dataRow = CSVFile.readLine(); // Read first line. // The while checks to see if the information is goose egg. If  // it is, nosotros've striking the end of the file. If not,  // process the data.  while (dataRow != nada) {     dataRow.split(",");     list.add together(dataRow);     dataRow = CSVFile.readLine();      // Read side by side line of information. } // Close the file once all data has been read. CSVFile.shut();  String[] tokens = null; Object[] test = list.toArray();  Cord[] stringArray = Arrays.copyOf(test, test.length, String[].class); //copies the object array into a String assortment   //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.separate(",");  }  //converts these Cord 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[ten][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 fault is when the ArrayList gets copied to the showtime Cord array merely I can't exist 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

  • +

#two CasiOo User is offline

Reputation: 1578

  • View blog
  • Posts: three,551
  • Joined: 05-April 11

Re: Converting CSV File into 2nd Array Coffee

Posted 26 Feb 2013 - eleven:03 PM

The split up method returns a String array
Remember that Strings are immutable, therefore a phone call to split will never alter the original String

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

#3 g00se User is offline

Reputation: 3744

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

Re: Converting CSV File into second Array Java

Posted 27 February 2013 - 04:37 AM

Quote

therefore a call to split will never change the original Cord

That is of form true, but even if it could, i'm wondering how information technology would exercise and 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 accept 25 elements each.

That'due south not what you hateful is it?
I ask, because further on there's confusion too:

Quote

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

You create an array capable of property 30 elements and then proceed to care for information technology every bit if it could concord 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 (annihilation else being invalid) then yous might also employ Scanner.nextInt to fill int[][] at the start

This post 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: ii,706
  • Joined: 29-July eleven

Re: Converting CSV File into 2D Assortment Coffee

Posted 27 February 2013 - 09:07 AM

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

#v pbl User is offline

Reputation: 8381

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

Re: Converting CSV File into second Array Coffee

Posted 27 February 2013 - 10:01 AM

As Java arrays exercise not take to exist rectangular y'all tin ever

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

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

#6 g00se User is offline

Reputation: 3744

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

Re: Converting CSV File into 2nd Array Java

Posted 27 February 2013 - xi:46 AM

Quote

Every bit Java arrays exercise not have to be rectangular yous can e'er...

Yeah only an array that's not rectangular equally a upshot of reading a csv file is a sign of a corrupt file ;)

meachamliffir.blogspot.com

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

0 Response to "How to Read a Csv File Into a 2d Array"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel