# Need help with an ArrayList please

• 11-08-2012, 02:28 AM
ndsmith20
Need help with an ArrayList please
Need to calculate the GPA and I am receiving an error. I am supposed to create a new class Courses to create an object that holds the credits and the grades but I do not know how to implement this so I am trying to bypass it. Here it was I have so far:

Code:

``` import java.util.*;   public class GPA   {       private int points;       private double credits;       private String grade;       private double totalPoints;       private double totalCredits;       private double gpa;       private int numCourses;       private int maxCourses;                 private int i;                   public GPA()       {         points = 0;         credits = 0;         totalCredits = 0;         totalPoints = 0;         grade = "";       }         public GPA(int courses)       {         numCourses = 0;         maxCourses = courses;         points = 0;         credits = 0;         totalCredits = 0;         totalPoints = 0;         grade = "";       } public void addCourse(int cr, String gr)       {                                        ArrayList<Integer> sumCredits = new ArrayList<Integer>(maxCourses);               ArrayList<Integer> sumPoints = new ArrayList<Integer>(maxCourses);                                                 sumCredits.add(cr);         grade = gr;         if((grade.equals("A"))||(grade.equals("a")))         {             points = 4;         }         else if((grade.equals("B"))||(grade.equals("b")))         {             points = 3;         }         else if((grade.equals("C"))||(grade.equals("c")))         {             points = 2;         }         else if((grade.equals("D"))||(grade.equals("d")))         {             points = 1;         }         else if((grade.equals("F"))||(grade.equals("f")))         {             points = 0;         }         else         {             points = -1;         }         sumPoints.add(points);                       totalCredits = totalCredits + sumCredits.get(numCourses);                 totalPoints = totalPoints + (sumPoints.get(numCourses)*sumCredits.get(numCourses));                         numCourses++;             }         public double calcGPA()       {                                 gpa = totalPoints/totalCredits;         return gpa;       } import java.util.*; public class Course {         private int credits;         private String grade;                 public Course(int cr, String gr)         {                 credits = cr;                 grade = gr;         }                 public String getGrade()         {                 return grade;         }                 public int getCredits()         {                 return credits;         } } import java.util.Scanner; public class GpaTestEx2 {         public static void main (String[] args)         {                 //declarations                 Scanner in = new Scanner(System.in); //input object                 int numCourses;        //number of courses - can be changed                 int credits;                //number of credits for a course                 String grade;                //grade for course                                                 //read in number of courses                 System.out.print("Enter number of courses: ");                 numCourses = in.nextInt();                                 //create Gpa object to hold specified number of courses                 GPA myGPA = new GPA(numCourses);                                                 //read in all courses and add course information to Gpa object                 for (int k=0; k<numCourses; k++)                 {                         System.out.print("Enter credits for course " + (k+1) + ": ");                         credits = in.nextInt();                         System.out.print("Enter grade for course " + (k+1) + ": ");                         grade = in.next();                                                 myGPA.addCourse(credits, grade);                 }                                 //print results                 System.out.println();                 System.out.printf("GPA is %4.2f%n", myGPA.calcGPA( ));         } //end main }```
This is my error:

Enter number of courses: 2
Enter credits for course 1: 4
Enter grade for course 1: a
Enter credits for course 2: 3
Enter grade for course 2: b
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)