Import Comma Delimited File to 2D Array
Hi All,
Goal: I will have varying sizes of a comma delimited file that I need to import into my Java program and store in a 2D array. Please note the examples will be much larger than the ones listed below. They will be on the order of 10,000 x 10,000.
I have the code to import the comma delimited file, however I cannot seem to figure out a way to write the code to store it in a 2D array, and have the array size vary with the size of the file.
Examples of comma delimited file that needs to go into a 2D array:
#
Name,1,2,3,4,5
1,1,1,1,1,1
2,1,1,1,1,0
3,1,1,1,1,0
4,1,1,1,1,1
5,0,1,1,0,0
6,1,1,1,1,0
7,0,1,1,0,0
Name,1,2,3,4,5,6,7,8,9,10
1,1,1,1,1,1,1,1,1,1,1
2,1,1,1,1,0,1,1,1,1,1
3,1,1,1,1,0,0,0,1,1,1
4,1,1,1,1,1,1,0,1,1,1
5,0,1,1,0,0,1,0,1,0,0
6,1,1,1,1,0,1,1,1,0,1
7,0,1,1,0,0,1,1,0,0,1
8,1,1,1,0,0,1,1,0,1,1
9,1,1,1,1,0,1,1,1,1,1
10,1,1,1,1,0,1,0,1,1,1
11,1,0,0,0,0,0,0,1,0,0
12,0,1,1,0,0,1,1,1,0,0
13,0,1,0,0,0,1,1,0,0,0
14,0,1,1,0,0,1,0,1,0,0
15,1,1,1,0,0,0,0,1,0,1
My current code:
Code :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CsvTest {
public void readFile(String filePath ) {
BufferedReader br = null;
int intExp;
try {
br = new BufferedReader(new FileReader(filePath));
String line = null;
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (br != null)
br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
CsvTest test = new CsvTest();
String path = "C:/comma_delim_test.csv";
test.readFile(path);
}
}
Re: Import Comma Delimited File to 2D Array
Quote:
have the array size vary with the size of the file.
Is there a direct relationship between the number of bytes in the file and the number of lines in the file?
If so, you could compute the array size from the file size.
If you read the first line, would that give you the size of each row and then file size / row size = nbr of rows
Otherwise you could make a wild guess of the size to define your array
OR you could use an ArrayList which will automatically expand as needed.
Re: Import Comma Delimited File to 2D Array
Great idea, I would never have thought of that. However, after some further thought, I can define the rows and columns at the start of the program, because I will know that information in advance.
With some further tinkering and web searches I have code to put the file contents into an array, however it is a 1D array and it inputs it into the array as a string and I need int values. I put comments in the trouble spots.
Code :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.Integer;
public class CsvTest {
public void readFile(String filePath ) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filePath));
String line = null;
while ((line = br.readLine()) != null) {
//how do i input the contents into a 2D array?
String [] values = line.split(",");
for (String str : values) {
//this is the code I'm trying to implement to convert the string to an int, but for some reason it does not work
int intExp = Integer.valueOf(str);
System.out.println(intExp);
}
System.out.println();
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (br != null)
br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
CsvTest test = new CsvTest();
String path = "C:/comma_delim_test.csv";
test.readFile(path);
}
}
Re: Import Comma Delimited File to 2D Array
The Integer class's parseInt method converts String to int
Re: Import Comma Delimited File to 2D Array
I tried Interger.parseIng(str) too, however I still get an error.
Re: Import Comma Delimited File to 2D Array
What error? Please post the full text of the error message.
What is the value of the str variable passed to the parseInt method?
Re: Import Comma Delimited File to 2D Array
Quote:
Originally Posted by
Norm
What is the value of the str variable passed to the parseInt method?
The value was "Name". Deleted the first line from the table and it works great now. Plus I think i have a way to get everything into a 2D array. Thanks for helping me through it.