-1

hey guys i am not sure how to fix the code below as i get an area out of bounds exception every time i run it any help would be greatly appreciated to fix this thx.

int numberStudents = numberOfStudents();  
    System.out.println("");
    int[] gradeOfStudent = gradeOfStudents(numberStudents);

method is below

public static int[] gradeOfStudents(int numberStudents) {
    Scanner input = new Scanner (System.in);
    int[] grades = new int [numberStudents];
    String[] name = new String[numberStudents];
    for(int i = 0; i < numberStudents;) {
        for(int j = 0; j < numberStudents; i++ ) {
            System.out.print("Enter the name of the " + (i + 1) + " student: ");
        name[i] = input.next();
            System.out.print("Enter the " + (i + 1) + " student's grade: " );
        grades[i] = input.nextInt();              
        }
    } 
    return grades;
}
Racon
  • 3
  • 2
  • Welcome to StackOverflow. Please be sure to add the whole exception stacktrace along with your question. – maloomeister Nov 27 '20 at 08:07
  • 2
    You're increment `i` in a loop that is conditioned on `j`, so that will **loop forever**. --- *Hint:* Get rid of the `j` loop, you should only have a regular `i` loop. – Andreas Nov 27 '20 at 08:14
  • Time to learn about the debugger, breakpoints and stepping through code ... :) – Fildor Nov 27 '20 at 08:15
  • I think you need to learn about how `for` loop works. I suggest you read [For loop in Java with example - BeginnersBook](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/) to understand what you did wrong. – Vincent C. Nov 27 '20 at 08:18

3 Answers3

0

You have a second loop inside your gradeOfStudents(int) method:

for(int j = 0; j < numberStudents; i++ ) {

As you can see, the loop variable is 'j' but it updates 'i' after every cycle - thus the i < numberStudents check in the first loop is no longer called for every 'i' -> it can be out of bounds.

You also don't seem to be using 'j' for anything, that whole loop might be redundant.

0

The issue is in your gradeOfStudents() method. You have 2 for loops.

for(int i = 0; i < numberStudents;) {
    for(int j = 0; j < numberStudents; i++ ) {

Your first loop runs numberOfStudents times, the second loop also runs numberOfStudents times.

The real problem here is, that you do i++ for each of those iterations. So you increase i to numberOfStudents * 2. But your arrays are only of size numberOfStudents.

There is actually no point in the second loop, as you don't need and apparently also don't use j. One loop is enough for this task. The fixed snippet would look like this:

public static int[] gradeOfStudents(int numberStudents) {
    Scanner input = new Scanner (System.in);
    int[] grades = new int [numberStudents];
    String[] name = new String[numberStudents];
    for(int i = 0; i < numberStudents; i++) {
        System.out.print("Enter the name of the " + (i + 1) + " student: ");
        name[i] = input.next();
            System.out.print("Enter the " + (i + 1) + " student's grade: " );
        grades[i] = input.nextInt();              
    }
    return grades;
}

But with this approach, you can see that you actually discard the name array completely. I don't think you want that.

So a more object-oriented approach would be, to create a class Student, which then holds the attributes grade and name.

So something like this:

class Student {

    private int grade;
    private String name;

    public Student(int grade, String name) {
        this.grade = grade;
        this.name = name;
    }
    
    // then provide getters and setters for the attributes

}

And then create and store a new Student(grade, name) for each of your inputs.

maloomeister
  • 2,461
  • 1
  • 12
  • 21
0

Remove your j for loop and add the i++ (increment i) to your first for loop

public static int[] gradeOfStudents(int numberStudents) {
Scanner input = new Scanner (System.in);
int[] grades = new int [numberStudents];
String[] name = new String[numberStudents];
for(int i = 0; i < numberStudents; i++) {
    System.out.println("Enter the name of the " + (i + 1) + " student: ");
    name[i] = input.next();
    System.out.println("Enter the " + (i + 1) + " student's grade: " );
    grades[i] = input.nextInt();              
} 
return grades;

}

xAqua
  • 11
  • 4