Reputation: 33
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
Upvotes: 3
Views: 320
Reputation: 4675
In case you require a double array, you should declare the array as double in the first place like this:
double[] asg1 = new double[100];
then you can use this:
asg1[i] = Double.parseDouble(array[2]);
Upvotes: 1
Reputation: 6181
Reason your code is giving you null is because value of i
is array.length
at following line.
System.out.println(name[i]);
Consider following example:
int i;
for(i=0;i<5;i++)
{
System.out.println(i);
}
System.out.println("value of i outside loop: "+i);
Output:
0
1
2
3
4
value of i outside loop: 5
So you are trying to get value at index 5
which is null.
To get the output you want using your current approach you can use following code:
for (i = 0; i < array.length; i++)
{
System.out.println(id[i] +":"+ name[i] +":"+ asg1[i] +":"+asg2[i]);
}
[Note: if your array size is not fix then you can use ArrayList
to store data, which has dynamic (grow-able) arraysize.]
Upvotes: 3
Reputation: 4675
I assume that your Information.txt file and ReadFile.java are in same default package I edited your program like this:
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(ReadFile.class.getResource(
"/Information.txt").getFile());
BufferedReader textReader = new BufferedReader(fr);
int i = 0;
String[] array = null;
// Read file in separate parts and remove commas
while ((sCurrentLine = textReader.readLine()) != null) {
array = sCurrentLine.split(",");
id[i] = array[0];
name[i] = array[1];
asg1[i] = array[2];
asg2[i] = array[3];
i++;
}
for(int j=0;j<i;j++){
System.out.println(id[j] + " , " + name[j] + " , " + asg1[j] + " , " + asg2[j]);
}
textReader.close();
}
// Catch exception if any
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Upvotes: 1
Reputation: 5187
The problem is that you're assigning values after you've read all of the input, rather than assigning one line at a time.
Instead of
//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];
}
you probably want something like
int i = 0;
while ((sCurrentLine = textReader.readLine()) != null) {
array = sCurrentLine.split(",");
id[i] = array[0];
name[i] = array[1];
asg1[i] = array[2];
asg2[i] = array[3];
i++;
}
It's also true that by the time you reach the System.out.println
in your current code, it'll be equal to one past the last index you populated, but that's a lesser issue.
Upvotes: 2