I am trying to make a program that performs some sorting on a 2D Array, but I am unable to find a way to put the data from the CSV file into the 2D Array, all it comes out is errors and im not sure how to fix it. I am aware of array lists but I do not want to use array lists here.
I would appreciate it if no changes were made to other parts of the program aside from things regarding the array
Data stored in CSV file:
Lana, 20, 6.3
Daniel, 30, 2.8
Lucas, 29, 4.9
Henry, 19, 8.2
Johnny, 59, 5.3
Code:
import java.util.*;
import java.io.*;
public class personProgram
{
public static void main(String[] args)
{
int rows = 5;
int cols = 3;
Person[][] persons = new Person[rows][cols];
readFile("PersonStorage.csv");
}
public static void readFile(String pFileName)
{
FileInputStream fileStream = null;
InputStreamReader rdr;
BufferedReader bufRdr;
int lineNum;
String line;
try
{
fileStream = new FileInputStream(pFileName);
rdr = new InputStreamReader(fileStream);
bufRdr = new BufferedReader(rdr);
lineNum = 0;
line = bufRdr.readLine();
while(line != null)
{
lineNum++;
processLine(line);
line = bufRdr.readLine();
}
fileStream.close();
}
catch (IOException errorDetails)
{
if(fileStream != null)
{
try
{
fileStream.close();
}
catch(IOException ex2)
{ }
}
System.out.println("Error in fileProcessing: " + errorDetails.getMessage());
}
}
private static void processLine(String csvRow)
{
String[] data;
data = csvRow.split(",");
int lineLength = data.length;
String name = data[0];
int age = (int)(data[1]);
double time = (double)(data[2]);
persons[row][0] = new Person(name, age, time);
rows++;
}
}