Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

java - program that processes grades on one string to get final grade

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)
question from:https://stackoverflow.com/questions/66051704/program-that-processes-grades-on-one-string-to-get-final-grade

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...