0
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.

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by NormR1
Featured Replies
  • 1

    [Code=Java] for(int i = 0; ; i++) { [/Code] 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: … Read More

1
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.

0

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

Edited by NormR1: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.