Brandon Bischoff
Brandon Bischoff

Reputation: 81

Java - Transferring Data from txt file into arrays

I am currently trying to work on a program that uses student ID's and GPA's (taken from a txt file) and uses these to do numerous other things like categorize the students into 1 of 8 categories based on GPA ranges, make a histogram of the students in each group, and also rank the students by GPA. The first thing I need to do however is transfer the student ID's and GPA's into two separate arrays.

I know the syntax for creating an array is as follows:

elementType[] arrayRefVar = new elementType[arraySize]

However, I still don't know how to pass the data that is read from the file into two separate arrays. The code I have to read the data from the txt file is as follows:

public static void main(String[] args) throws Exception  // files requires exception handling
{
    String snum;     
    double gpa;
    Scanner gpadata = new Scanner(new File("studentdata.txt"));

    while (gpadata.hasNext()) // loop until you reach the end of the file 
    {
        snum = gpadata.next(); // reads the student's id number
        gpa = gpadata.nextDouble(); // read the student's gpa

        System.out.println(snum + "\t" + gpa); // display the line from the file in the Output window

    }
}

So my question is: how do I pass this information into two separate arrays? I apologize if my question is hard to understand, I am extremely new to programming. I have been stumped on this program for a long time now, and any help would be extremely appreciated! Thank you.

Upvotes: 0

Views: 199

Answers (1)

C-Shark
C-Shark

Reputation: 821

You can create two arrays before the while loop, and then add each element inside the loop to each array. But there is one problem with this approach: we don't know the number of values, thus we cannot create a fixed-sized array for this. I suggest to use ArrayList instead, which can grow as needed. Something like this:

public static void main(String[] args) throws FileNotFoundException {

    Scanner gpadata = new Scanner(new File("studentdata.txt"));
    List<String> IDs = new ArrayList<>();
    List<Double> GPAs = new ArrayList<>();
    while (gpadata.hasNext()) // loop until you reach the end of the file
    {
        String snum = gpadata.next(); // reads the student's id number
        double gpa = gpadata.nextDouble(); // read the student's gpa

        IDs.add(snum);
        GPAs.add(gpa);

        System.out.println(snum + "\t" + gpa); // display the line from the file in the Output window
    }
    // Use IDs and GPAs Lists for other calculations
}

A more better approach to use a Map to "pair" a GPA to a Student ID.

Edit:

After you clarified that the max record number never be more than 1000, I modified my solution to use arrays instead of Lists. I didn't change the variable names so you can compare the solutions easily.

public static void main(String[] args) throws FileNotFoundException {

    Scanner gpadata = new Scanner(new File("studentdata.txt"));
    String[] IDs = new String[1000];
    double[] GPAs = new double[1000];
    int counter = 0;

    while (gpadata.hasNext()) // loop until you reach the end of the file
    {
        String snum = gpadata.next(); // reads the student's id number
        double gpa = gpadata.nextDouble(); // read the student's gpa

        IDs[counter] = snum;
        GPAs[counter] = gpa;

        System.out.println(snum + "\t" + gpa); // display the line from the file in the Output window
        counter++;
    }
    // Use IDs and GPAs Lists for other calculations
}

Note that we need a counter (aka. index) variable to address the array slots.

Upvotes: 1

Related Questions