overduealteration
overduealteration

Reputation: 11

Method not changing array in main

First time posting here but I have a question in regards to a method not changing the values in the array it is given.

I thought arrays were passed by reference and thus changing the array in the method would change it in the array. However this is not working for me and I have no idea why.

My code is below. The method that is not working is readFile()

package StudentsGrades;

import java.io.*;
import java.lang.*;

public class StudentsGrades {

    public static void main(String [] args ) {
    int numberOfLines = 0;
    String fileName = "";

    fileName = "marks_file.csv";
    //Obtain the number of lines in the given csv.
    numberOfLines = getNumberLines(fileName);
    System.out.println(numberOfLines);

    //initialise the arrays that the data will be stored in.
    double[] gradesArray = new double[numberOfLines];
    String[] studentsArray = new String[numberOfLines];

    if (numberOfLines > 0) {
        readFile(studentsArray, gradesArray, numberOfLines, fileName);
    }
    System.out.println(studentsArray[4]);
}
public static int getNumberLines (String importFile)  {
    int numLines = 0;
    try {
        FileReader fnol = new FileReader(importFile);
        LineNumberReader lnr = new LineNumberReader(fnol);

        while (lnr.readLine() != null ) {
            numLines++;
        }

    } catch (FileNotFoundException fe) {
        System.out.println("The file cannot be found");
    } catch (Exception e) {
        System.out.println("Invalid");
    }
    return numLines;
}
public static void readFile (String [] studentsArray, double[] gradesArray, int numLines, String fileName ) {
    try {
        String lineData = null;
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);

        String currentLine = "";

        while ((currentLine = br.readLine()) != null ) {
            //Store the current Line in a string
            String[] lineDataArray = currentLine.split("\\s*,\\s*");
            //To index its position in the array
            int index = 0;
            //System.out.println(lineDataArray[1]);
            studentsArray[index] = lineDataArray[0];
            gradesArray[index] = Double.parseDouble(lineDataArray[1]);
            System.out.println("Student: " + studentsArray[index]
            + " Grade: " + gradesArray[index]);
            index++;
        }
    } catch (Exception e) {
        System.out.println("Unexpected value in file.");
    }

}
}

The output

Student: Christopher Lee Grade: 54.0 Student: Stanley Wright Grade: 90.5 Student: Oliver Stewart Grade: 75.8 Student: Jessica Chang Grade: 34.65 Student: Adam Bweon Grade: 66.6 Student: Chrissy Yeo Grade: 88.9 null

As you can see the last value is null and that is when I try and print a value from the array within Main.

Upvotes: 1

Views: 386

Answers (1)

kkica
kkica

Reputation: 4104

Place int index=0; outside the loop.

If you want to fill the arrays you need to declare index before the loop. Since now it is inside the loop, it is redeclared everytime and initialized to 0. So, index is always 0 when you use it, and you overwrite the values.

Upvotes: 5

Related Questions