import java.util.ArrayList;

public class CSStudent 
{
	String[] requiredGeneralClasses = {"biology", "calculus", "chemistry", "english", "physics", "psychology", "economics"};
	String[] requiredInMajorClasses = {"algorithms", "data structures", "numerical analysis", "graphics, databases"};
	String[] completedClasses;
	int[] gradeForCompletedClasses;
	
	public CSStudent()
	{
		completedClasses = new String[10000];
		gradeForCompletedClasses = new int[10000];
	}
	
	public void completeClass(String classs /*class*/, int gradeOnHundredPointScale)
	{
		int count = 0;
		//while(true)
		//{
		for(int i = 0; ; i++)
		{
			count++;
			if(classs.equals(requiredGeneralClasses[count++]) || classs.equals(requiredInMajorClasses[count++]))
			{
				completedClasses[count] = classs;
			}
			else
			{
				completedClasses[count] = classs;
			}
			
			if(gradeOnHundredPointScale <= 100)
			{
				gradeForCompletedClasses[count] = gradeOnHundredPointScale;
			}
		}
	}
public class CSStudentTester 
{
	public static void main(String[] args)
	{
		CSStudent student = new CSStudent();
		student.completeClass("biology", 85);
		//System.out.println(student.getInMajorGPA());
	}
}

ok so I have the above code and I keep getting error for the completeclass method:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at CSStudent.completeClass(CSStudent.java:22) //if(classs.equals(requiredGeneralClasses[count]) || classs.equals(requiredInMajorClasses[count]))
at CSStudentTester.main(CSStudentTester.java:7) //student.completeClass("biology", 85);


can anyone please tell me what am I doing wrong? also I am confused on how should I write the major gpa method. if possible could anyone please write some steps that explain how that method can be done.

for(int i = 0; ; i++)
{

here's your initial mistake. you keep running this for loop, without it ever stopping, since you don't set an expression which decides until what result to iterate over the block.
also, take a look at the first lines within your for-loop:

count++;
if(classs.equals(requiredGeneralClasses[count++]) || classs.equals(requiredInMajorClasses[count++]))
{
completedClasses[count] = classs;
}

you do know that completedClass[count] = classs;
starts with the index 1 instead of 0? I assume this is a logical error in your coding.

basically, an ArrayIndexException means that you (just a for instance) declared an array which takes three elements, but either are requesting or are trying to store data in the fourth element.

That's an interesting usage of the count variable as an index. What value do you expect it to have on line 26?

Edited 4 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.