Question
I get this Error Part3 - Find the students with highest GPA ------------------------------------------------ Exception in thread main java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length
I get this Error
Part3 - Find the students with highest GPA
------------------------------------------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at StudentShuffle.getStudentsWithPerfectGPA(StudentShuffle.java:113)
at StudentShuffle.main(StudentShuffle.java:146)
HERE IS THE CODE, PLEASE FIX the problem WITHOUT CHANGING A LOT OF CODE.
Student.java
public class Student { private String name; private String major; private double gpa; public Student(String name, String major, double gpa) { this.name=name; this.major=major; this.gpa=gpa; // TO-DO: Assign the given parameters to the data fields. Use the this keyword. } public double getGPA() { // TO-DO: return this.gpa return this.gpa; } public String getName() { // TO-DO: return this.name return this.name; } // toString method: this is a handy way to specify what you want it to look like when you // print a Student object. Without this, printing a student using System.out.println(student) // will print a memory address. The toString method is called implicitly (you don't have to // call it explicitly when you print) public String toString() { return "[" + name + ", " + major + ", " + gpa + "]"; } }
StudentShuffle.java
import java.util.Scanner; import java.io.*;
public class StudentShuffle {
private Student[] studentArray;
public StudentShuffle() { this.studentArray = new Student[15]; }
public Student[] getStudentArray() { return this.studentArray; } int index; String line; public void getDataFromFile() { Scanner myScanner; /* TO-DO: write a method to get data from the text file, store the data in * Student objects, and store the students in an array. * Suggestion: use Scanner myScanner = new Scanner(new FileReader("students_list.txt")) * to access the file. * Write your code inside the try block (you can leave the catch block as-is). * Some useful methods: Scanner hasNextLine(), String split(), Double.valueOf() */ // You may initialize some variables here try { // Your code here myScanner = new Scanner(new FileReader("students_list.txt")); while(myScanner.hasNextLine()){ line = myScanner.nextLine(); System.out.println(line); String [] arraystring = line.split(" "); if(arraystring.length == 3){ String name = arraystring[0]; String major = arraystring[1]; double gpa =Double.parseDouble(arraystring[2]); Student student=new Student(name,major,gpa); studentArray[index] = student; index++; } } } catch(FileNotFoundException e){ System.out.println(e.getMessage()); } }
public void printStudents(Student[] students) {
for(int i=0 ; i<14 ; i++) { if(students[i]!=null) system.out.println(students[i]); }>
public double findLowestGPA() {
double lowest=200; for(int i=0 ; i<14 ; i++) if(studentarray[i]!=null){ { if(studentarray[i].getgpa() // Your code here } return lowest; } public Student[] getStudentsWithPerfectGPA() { // TO-DO: write code that finds the students with perfect GPA (4.0), // adds those students to a new array, and returns that array // Hint: use the method getGPA from Student class // Your code here double high=4.0; int count=0; for (int i = 0; i < studentArray.length-1; i++) { if(studentArray[i].getGPA()==high) count++; } Student[] student = new Student[count]; for (int i = 0; i public static void main(String[] args) { // TO-DO: implement your main method (see notes below) System.out.println(" Part1 - Get data from file and print students"); System.out.println("------------------------------------------------"); StudentShuffle shuffle = new StudentShuffle(); shuffle.getDataFromFile(); // Instantiate a StudentShuffle object. // Call the method getDataFromFile() using the StudentShuffle object, // for example shuffle.getDataFromFile(). // Call shuffle.printStudents(shuffle.getStudentArray()) to print the students System.out.println(" Part2 - Find the lowest GPA value"); System.out.println("------------------------------------------------"); // Call findLowestGPA() method, save the value returned, and print it System.out.print("the lowest GPA is "); System.out.println( shuffle.findLowestGPA()); System.out.println(" Part3 - Find the students with highest GPA"); System.out.println("------------------------------------------------"); Student[] tempArray=shuffle.getStudentsWithPerfectGPA(); shuffle.printStudents(tempArray); // Call getStudentsWithPerfectGPA() method, save the returned array, // and use your printStudents method to print the good students } } students_list.txt Brenna Computer_Science 4.0 Janet Biology 3.4 Sandeep Physics 3.9 Grant Marketing 4.0 Wei Business 3.7 Andre Communications 3.7 Fernanda Math 3.9 Jonas Physiology 2.9 Grace Fine_arts 3.5 Stephanie Chemistry 4.0 Mohammed Business 3.9 Nozomi Computer_Science 3.5 Aparna Literature 3.7 Ayesha Business 3.5 thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started