0
Scanner scan = new Scanner(System.in);
        
        String num;
        
        String again = "yes";
        
        System.out.println("Welcome to the Grade Finalization Center");
        System.out.println("Want to continue?: ");
        String start = scan.nextLine();
        
        
            do {    
                // Start grade finalization // 
            
        System.out.println("Starting Program....");
        
        System.out.print("Student's name: ");
        String name = scan.next();
        
        System.out.println("Student's grades separated by space (A1 A2 EX P): ");
        num = scan.next();
        
        //String r1 = num.substring(0, 2);
        //int r11 = Integer.parseInt(r1);
        //double A1 = (r11*0.25);
            
        String r2 = num.substring(3, 4);
        //int r12 = Integer.parseInt(r1);
        //double A2 = (r12*0.25);
    
        System.out.println("Final grade is " + r2);
        
        
        
            
        
        System.out.print("Want to continue with another student?: ");
        again = scan.next();
        
        } while (again.equalsIgnoreCase("yes"));
            

getting the error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 3, end 4, length 2
    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734)
    at java.base/java.lang.String.substring(String.java:1903)
    at W4.main(W4.java:34)
Aalexander
  • 4,834
  • 3
  • 7
  • 30
Brxaan J
  • 1
  • 4
  • Prompt and read user’s input for the student’s name, assignment 1 grade (A1), assignment 2 (A2), exam (EX), and participation grade (P). User Scanner to read input. Each grade input should be 0-100 and the final grade should be calculated as follows: A1*0.25+A2*0.25+EX*0.4+P*0.1 Output the student’s information and the calculated course grade prompt user whether they want to calculate grade for another student and repeat the input/output processing Allow user to exit program without inputting student’s data – Brxaan J Feb 04 '21 at 18:33
  • if the length of the `num` string is less then 4 then you'll get the error! – XO56 Feb 04 '21 at 18:36
  • it shouldn't be less than four because there are 5 sets of numbers – Brxaan J Feb 04 '21 at 18:37

2 Answers2

0

The error message tells you that you are trying to access parts of a String that don't exist. You are trying to get positions 3-4 from a String that only has a length of 2, which is obviously impossible and therefor won't work.

Now the question for you is: Why is the String num only with length 2 at the point where you call

String r2 = num.substring(3, 4);

Your problem is how you try to read your String from user input:

 System.out.println("Student's grades separated by space (A1 A2 EX P): ");
 num = scan.next();

You clearly want the user to enter something that contains spaces, but that will not work by default with the next() method of Scanner.

I recommend reading the official documentation of the scanner class you use.

You'll see in the description of the class:

"A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace."

and in the documentation of the next() method you use:

"Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern."

This tells you that by default the method next() will only read the input until it reaches the first whitespace. So currently if you input "A1 A2 EX P" the resulting String will just be "A1" because then the delimiter is reached and the next() method thinks it has done its job.

Instead of the next() method you should use the method nextLine() so that your input will be able to contain spaces.

OH GOD SPIDERS
  • 2,399
  • 2
  • 11
  • 15
  • once I change it to nextLine it skips the entry and errors to Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 3, end 4, length 0 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.substring(String.java:1903) at W4.main(W4.java:33) – Brxaan J Feb 04 '21 at 19:12
  • That is because of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). You should change all your calls of `next()` to `nextLine()` including the part where you ask for student name. – OH GOD SPIDERS Feb 04 '21 at 19:20
0

Actually, I have not fully understood what you expect to get from the result. Anyway, let's focus just on the statement which is causing the exception, in particular let's look at the following lines:

System.out.println("Student's grades separated by space (A1 A2 EX P): ");
num = scan.next();
String r2 = num.substring(3, 4);

In a nutshell, the String variable named r2 will be assigned with a substring of num, which was filled in previous line by the value given in the standard input by the user. That's OK, but be careful when using the method substring.
Here is the definition:
public String substring(int beginIndex, int endIndex)
So, the method extracts a sub string of a given one, which means just a portion of it, starting from the beginIndex (in your case 3), and ending with the endIndex (in your case 4), where the second is "exclusive", which means that the character at that index won't be included in the returned string. It is essential to rember that the given string (which is "num") mast have a lenght at least equals to the endIndex, otherwise it will always throws a StringIndexOutOfBoundsException, which means that it has been exceeded the margin of the string.

So, your program needs a bit of improvement, in order to support these scenarios.

First of all, I suggest that you replace all scan.next() statements with scan.nextLine(), so you always makes the program read what the user has inserted until he pressed "Enter", whilst the method scan.next()just returns the first token found in the string, and because you had used the constructor Scanner(System.in), it will use the "default" delimiter to match a token, which is the "whitespace".
Secondly, if your intention is just to get the last grade wrote by the user, you can do that by simply using the following statement to assign the variable "r2":

String r2 = !num.isEmpty() ? num.substring(num.length()-2, num.length()).trim() : "none";

You can notice that I used also a ternary operator to include the case when the user doesn't insert any grade, so you can print out "none".

Try this and see how it works.

Marco Tizzano
  • 1,475
  • 8
  • 15