Home > Software engineering >  Creating a regular array rather than an ArrayList
Creating a regular array rather than an ArrayList

Time:11-01

So I'm working on a project where I have to read student information from a text file into an array and then manipulate it where I can find the highest, lowest, and average GPA in the array, as well as printing all values within the array. However, I realized that the project specs do not allow me to use ArrayLists, even though I already used ArrayLists. It's important that I create a student object so that the rest of my code will operate. Is there anyway I can convert my code to an alternative method that will do the same thing? Below is the code that needs to be modified.

// The file and scanner classes will read info from the StudentData.txt file to create the student object.
File inputFile= new File("StudentData.txt"); 
Scanner fileReader= new Scanner(inputFile);

ArrayList<Student> roster= new ArrayList<Student>(100); // This ArrayList will store the students as the roster variable.

/*
 * This while loop will read the StudentData.txt file until the end and add the First & Last Name and GPA into a line within
 * the roster array.
 */
while (fileReader.hasNextLine()) {
    String firstName = fileReader.nextLine();
    String lastName = fileReader.nextLine();
    String GPA = fileReader.nextLine(); 
    Student student = new Student(firstName, lastName, Double.parseDouble(GPA));
}

I tried converting it to a String, but I believe I was doing it wrong and the rest of my code had errors.

CodePudding user response:

You must allocate enough space in the array to hold all the Student objects and keep track how many you have:

int count = 0;
Student[] roster = new Student[100];
    
while (fileReader.hasNextLine()) {
    String firstName = fileReader.nextLine();
    String lastName = fileReader.nextLine();
    String GPA = fileReader.nextLine(); 
    Student student = new Student(firstName, lastName, Double.parseDouble(GPA));
    roster[count  ] = student;
 }

Then you can loop over the students to calculate the various statistics:

for (int i = 0; i < count; i  ) {
    // do something with roster[i]
}

CodePudding user response:

public static Student[] readStudentsFromFile(File file) throws FileNotFoundException {
    // file should be released
    try (Scanner scan = new Scanner(file)) {
        // important when read double (`.` or `,`)
        scan.useLocale(Locale.ENGLISH);

        // in case we do not know how many data in the file, use list
        // if we know, then do use array right here
        List<Student> students = new ArrayList<>();

        while (scan.hasNext()) {
            String firstName = scan.nextLine();
            String lastName = scan.nextLine();
            double gpa = scan.nextDouble();
            students.add(new Student(firstName, lastName, gpa));
        }

        // convert list to array
        return students.toArray(Student[]::new);
    }
}

public static class Student {

    private final String firstName;
    private final String lastName;
    private final double gpa;

    public Student(String firstName, String lastName, double gpa) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.gpa = gpa;
    }
}

CodePudding user response:

You can treat the array as if it were a variable length list by using Arrays.copyOf(). This is what ArrayList does internally. You can optimize processing time and memory requirements by adjusting the initial size and increment of the array.

int count = 0;
Student[] roster = new Student[10];
File inputFile = new File("StudentData.txt");
try (Scanner fileReader = new Scanner(inputFile)) {
    while (fileReader.hasNextLine()) {
        String firstName = fileReader.nextLine();
        String lastName = fileReader.nextLine();
        String GPA = fileReader.nextLine();
        if (count >= roster.length)
            roster = Arrays.copyOf(roster, roster.length   10);
        roster[count  ] = new Student(firstName, lastName, Double.parseDouble(GPA));
    }
}
  • Related