Hey guys, this is my first time on here so I don't know if my code is going to come out messy, so if it does I'm sorry. I'm new to java programming and I'm working on an assignment where I was given the main method and one constructor class, and I had to create the other constructor class myself. However when I run the program, the main method complains at the line:
int size = Integer.parseInt(args[0]);
And says "java.lang.ArrayOutOfBoundsException: 0" I'm not sure what this means, but here is my constructor class StudentCollection along with the main method if that helps. Could anyone tell me what I am doing wrong?
public class StudentCollection { private double averageGPA; private int count; private Student[] collection; public StudentCollection(int initialSize){ collection=new Student[initialSize]; count=0; } public void addToCollection(Student s) { if(count==collection.length) { increaseSize(); } collection[count]=s; count++; } private void increaseSize() { Student[] collection2=new Student[collection.length*2+1]; System.arraycopy(collection, 0, collection2, 0, count); collection=collection2; } public double getAverageCreditsCompleted() { double sum=0; if(count>0) { for(int i=0; i<count; i++) { sum=sum+collection[i].getCreditsCompleted(); } return sum/count; } else return 0; } public double getAverageGPA() { double sum=0; if(count>0) { for(int i=0; i<count; i++) { sum=sum+collection[i].getGPA(); } return sum/count; } else return 0; } public int getCount() { return count; } public boolean removeFromCollection(java.lang.String name) { for(int i=0; i<count; i++) { if(collection[i].getName().toUpperCase().equals(name.toUpperCase())) { collection[i]=collection[count-1]; count--; return true; } else return false; } return false; } public String toString() { String stuString=""; for(int i=0; i<count; i++) { stuString=stuString+collection[i]; } return stuString; } }
And here is the main method:
public class Lab8 { public static void main (String[] args) { // Scanner to read input from the keyboard Scanner keyboard = new Scanner(System.in); int size = Integer.parseInt(args[0]); // use "2" when you run the program StudentCollection collection = new StudentCollection(size); Student s; // s is a Null reference at this point String name, major; int credits; double gpa; int choice; DecimalFormat df = new DecimalFormat("0.00"); String line; do { System.out.println(); System.out.println("\t1. Add a new Student to the collection"); System.out.println("\t2. Remove a Student from the collection"); System.out.println("\t3. Display the students in the collection"); System.out.println("\t4. Display statistics"); System.out.println("\t5. Quit the program"); System.out.print("\nYour selection: "); choice = Integer.parseInt(keyboard.nextLine() ); switch(choice) { case 1: System.out.print("Enter the student's full name: "); name = keyboard.nextLine(); System.out.print("Enter the students gpa: "); line = keyboard.nextLine(); gpa = Double.parseDouble(line); System.out.print("Enter the student's major: "); major = keyboard.nextLine(); System.out.print("Enter the number of credits completed by the student: "); line = keyboard.nextLine(); credits = Integer.parseInt(line); s = new Student(name, gpa, major, credits); collection.addToCollection(s); break; case 2: System.out.print("Enter the name of the student that is to be removed: "); name = keyboard.nextLine(); boolean result = collection.removeFromCollection(name); if (result) System.out.println("Student with name " + name + " is successfully removed"); else System.out.println("There is no Student whose name is " + name); break; case 3: System.out.println(collection); break; case 4: System.out.println("There are " + collection.getCount() + " students in the collection"); System.out.println("Average GPA of the students in the collection: " + df.format(collection.getAverageGPA() )); System.out.println("Average number of credits completed by the students " + "in the collection: " + collection.getAverageCreditsCompleted() ); System.out.println(); break; case 5: System.out.println("Thank you for using my program. Good bye!"); System.exit(0); // quit the program default: System.out.println("Illegal Choice. Try again"); } // end of switch } while (choice != 5); } // main method }
Thank you, I appreciate the help.