0

I am having trouble debugging the following code so that the input is 4 people and 4 separate grades with spaces in between that returns the student's grade based on the highest score, but I continuously get an error. Somebody please help. Thank You!

Output:

Enter the number of students: 4 
Enter 4 scores: 55 48 70 58 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 
     Index 4 out of bounds for length 4 at Ex7_1.main(Ex7_1.java:15) 
import java.util.*;
public class Ex7_1 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the number of students: ");

    int num = Integer.parseInt(in.nextLine()) + 1;
    System.out.printf("Enter %d scores: ", 4);

    String input = in.nextLine();
    String[] sArray = input.split(" ");
    int[] array = new int[num];

    for (int i = 0; i < num; i++) {
      String sNum = sArray[i];
      array[i] = Integer.parseInt(sNum);
    }

    int highest = 0;

    for (int i = 0; i <= num; i++) {
      if (array[i] >= highest) {
        highest = array[i];
      }
    }

    for (int i = 0; i <= num; i++) {
      int score = array[i];
      if (score >= (highest - 10)) {
        System.out.printf("Student %d score is %d and grade is A", i, score);
      } else if (score >= (highest - 20)) {
        System.out.printf("Student %d score is %d and grade is B", i, score);
      } else if (score >= (highest - 30)) {
        System.out.printf("Student %d score is %d and grade is C", i, score);
      } else if (score >= (highest - 40)) {
        System.out.printf("Student %d score is %d and grade is D", i, score);
      } else {
        System.out.printf("Student %d score is %d and grade is F", i, score);
      }
    }
  }
}

This code presents an error after making the <= to just < in both cases. I do not know how to solve that error. Please help. Thank You!

Gary Zlobinskiy
  • 93
  • 1
  • 10
  • 1
    What error? Please provide an [MCVE] – GBlodgett Mar 19 '19 at 00:17
  • Enter the number of students: 4 Enter 4 scores: 55 48 70 58 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4 at Ex7_1.main(Ex7_1.java:15) – Gary Zlobinskiy Mar 19 '19 at 00:22
  • How would I solve this problem? – Gary Zlobinskiy Mar 19 '19 at 00:28
  • That did solve one problem, but after I fix that problem, another one arises that I can't seem to fix, this is the error: Enter the number of students: 48 55 70 58 Exception in thread "main" java.lang.NumberFormatException: For input string: "48 55 70 58" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at Ex7_1.main(Ex7_1.java:7) – Gary Zlobinskiy Mar 19 '19 at 00:35
  • How would I solve this problem? Thank you! – Gary Zlobinskiy Mar 19 '19 at 00:36
  • `int num = Integer.parseInt(in.nextLine()) + 1;` looks weird. I guess you are trying to get the number of input scores? If that is the case, you can use `int num = sArray.length;` after `String[] sArray = input.split(" ");` And, in java, for an array of length 4, the index are 0, 1, 2 and 3, so you should use < instead of <= – user10253771 Mar 19 '19 at 00:48
  • Thank you but how do I solve the error in the previous comment – Gary Zlobinskiy Mar 19 '19 at 00:51
  • Sorry, I didn't see that you need to enter the number of students. Could you please change line 7 into the following three lines: first line: `String numStr = in.nextLine();` , second line: `System.out.println(numStr);` , third line: `int num = Integer.parseInt(numStr) + 1;`, and reproduce the error. Tell me what is printed out in the console. – user10253771 Mar 19 '19 at 00:55
  • According to the error message, it seems that you typed "48 55 70 58" for "Enter the number of students:", so you just forgot to type 4 (meaning 4 students) before input their scores. – user10253771 Mar 19 '19 at 01:05

3 Answers3

0

It seems your problem isn't in either of the sections where you look through the array, but rather where you set the contents of the array.

for (int i = 0; i < num; i++) {
  String sNum = sArray[i];
  array[i] = Integer.parseInt(sNum);
}

It seems that in this section, you try to access the item at index 4 of sArray, which does not exist, as the array has a length of 4, and thus a maximum index of 3.

Additionally, you are adding 1 to num near the beginning, which isn't necessary if you use < instead of <= in the for-loops, and is causing you to use indexes 1 through 4, instead of 0 through 3 as you likely want.

int num = Integer.parseInt(in.nextLine()) + 1; // You don't need this +1

Finally, that print statement where you ask for a number of scores will always print out 4, no matter how many you actually need. You likely want to pass num into that print statement, so it actually prints out how many scores you're expecting.

System.out.printf("Enter %d scores: ", 4); // Replace this 4 with num

Hope this helps!

0

You have used <= operator in every for loop. In beginning, almost everybody get confused in this part Arrays start with 0 index. So you don't have to use >=, use this instead > in your for loops.

Secondly, if your always entering marks of 4 students, then don't ask this:

System.out.println("Enter the number of students: ");

What if user input "5". Your will go like:

Enter the number of students: 5
Enter 4 scores:

So, this doesn't make a good sense. At last put a \n in your printing segment of the code, it'll make the output more clearer and easy to read:

System.out.printf("\nStudent %d score is %d and grade is A", i, score); // "\n" added
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
0

To avoid ArrayIndexOutOfBoundException :-

int num = Integer.parseInt(in.nextLine()) + 1; //remove + 1 from this line
use < instead of <= in the for-loops

To avoid NumberFormatException :-

Trim your string and while splitting use regex which matches any number of whitespace characters.
\\s+ - matches sequence of one or more whitespace characters.

Try below code:

        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of students: ");

        int num = Integer.parseInt(in.nextLine().trim());
        System.out.printf("Enter %d scores: ", num);

        String input = in.nextLine().trim();
        String[] sArray = input.split("\\s+");
        int[] array = new int[num];

        for (int i = 0; i < num; i++) {
            String sNum = sArray[i];
            array[i] = Integer.parseInt(sNum);
        }

        int highest = 0;

        for (int i = 0; i < num; i++) {
            if (array[i] >= highest) {
                highest = array[i];
            }
        }

        for (int i = 0; i < num; i++) {
            int score = array[i];
            if (score >= (highest - 10)) {
                System.out.printf("Student %d score is %d and grade is A\n", i+1, score);
            } else if (score >= (highest - 20)) {
                System.out.printf("Student %d score is %d and grade is B\n", i+1, score);
            } else if (score >= (highest - 30)) {
                System.out.printf("Student %d score is %d and grade is C\n", i+1, score);
            } else if (score >= (highest - 40)) {
                System.out.printf("Student %d score is %d and grade is D\n", i+1, score);
            } else {
                System.out.printf("Student %d score is %d and grade is F\n", i+1, score);
            }
        }
    }
Harishma A
  • 61
  • 6