3

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

4 Answers 4

3

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.]

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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());
    }
}

}

Comments

1

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]);

2 Comments

It says "Error: For input string: "Asg1" Any idea what that means?
This means parseDouble is not reading a double value. This error may occur because of the very first line. You need to put an if() to make sure parseDouble works on all lines except the first line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.