I have to read a text file in Java, separate its content into separate arrays, and then compute the average of certain arrays. My problem is that when I try to display a certain array to make sure it works, I get "null" instead of the whole list. Can anyone help me find where I went wrong?
import java.io.*;
class ReadFile {
public static void main(String args[])
String[] id = new String[1234]; // random number just for initialization
String[] name = new String[1234];
String[] asg1 = new String[1234];
String[] asg2 = new String[1234];
try {
String sCurrentLine;
FileReader fr = new FileReader("Information.txt");
BufferedReader textReader = new BufferedReader(fr);
int i;
String[] array = null;
// Read file in separate parts and remove commas
while ((sCurrentLine = textReader.readLine()) != null) {
array = sCurrentLine.split(",");
System.out.print(array[0]);
System.out.print(array[1]);
System.out.print(array[2]);
System.out.println(array[3]);
}
// enter variables from each line in separate arrays
for (i = 0; i < array.length; i++) {
id[i] = array[0];
name[i] = array[1];
asg1[i] = array[2];
asg2[i] = array[3];
}
System.out.println(name[i]);
// "null" appears instead of all the names
textReader.close();
} catch (Exception e) {
// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
The input looks like this:
ID, name, Asg1, Asg2
123456, Max, 98.00, 80.00
012345, James, 40.00, 69.00
234567, Mary, 78.00, 88.00