Results 1 to 4 of 4
  1. #1
    jmanv888 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default While loop with case - getting error

    I am trying to write a program that will ask the user how many classes they had for a semester. It proceeds to present the user with 5 options for the grade they received A,B,C,D,F. What I am trying to do is a while loop that counts down while the user inputs his grades for the number of classes he/she took that semester. I am able to compile the code but when I type in how many classes in the semester I receive the error " Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out o range: 0 at java.land.String.CharAt<Unknown Source> at GPA.main<GPA.java:38> Any help would be awesome and it is deeply appreciated.

    Thank you,
    Jason

    Java Code:
    import java.util.Scanner;
    
    public class GPA {
        public static void main(String[] args) {
    
    	Scanner input = new Scanner(System.in);  
    
    	int CounterNew=0;  
    	
    	
    	int CountA=0;
    	int CountB=0;
    	int CountC=0;
    	int CountD=0;
    	int CountF=0;
    	
    	System.out.println("Enter how many classes you took: ");
    	int Counter = input.nextInt();
    
    
            
    	System.out.println("\n\nEnter the grade you recieved from the list: ");
    	System.out.println("A");
    	System.out.println("B");
    	System.out.println("C");
    	System.out.println("D");
    	System.out.println("F");
    
    
    
    	
    	String choice = input.nextLine();
    	char ch = choice.charAt(0); 
            
    	while (Counter > 0){
    	switch (ch)
    
    	{
    	case 'A': 
    	case 'a': 
    	System.out.println("A = 4.0");
            CountA++;
    	break;
    
    	case 'B': 
    	case 'b':
    	System.out.println("B = 3.0");
            CountB++;
    	break;
    
    	case 'C': 
    	case 'c':
    	System.out.println("C = 2.0");
            CountC++;
    	break;
    
    	case 'D': 
    	case 'd':
    	System.out.println("D = 1.0");
            CountD++;
    	break;
    
    	case 'F': 
    	case 'f':
    	System.out.println("F = 0.0");
            CountF++;
    	break;
    
    	default:
    	System.out.println("Hey dummy, enter only a A, B, C, D, or F!");
            }
            
    	{
    		Counter--;
    		CounterNew++;
    	}	
            }
    	{
    		System.out.println("Your average is: " +  "%");
    	}
       }
    }

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    Dude

    Its because you have entered empty string from keyboard.. Make sure what string you have typed by SOP.
    Mak
    (Living @ Virtual World)

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    You have fallen foul of the Scanner.nextInt problem. If user enters 48<enter> then the nextInt method only reads the 48 and leaves the carriage return in the buffer. No what you have to remember is that there is a String between the 8 and the carriage return, it just happens to be an empty String or a String with a length of 0. So when you call nextLine it returns that empty String to your choice variable. Of course then trying to extract the char at position 0 will throw the exception becuase it does not exist.

    Quick and dirty solution call nextLine and throw it away immediately after the call to nextInt.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Quote Originally Posted by makpandian View Post
    Dude

    Its because you have entered empty string from keyboard.. Make sure what string you have typed by SOP.
    No it isn't.

Similar Threads

  1. junit test case error
    By acmohan in forum New To Java
    Replies: 7
    Last Post: 07-12-2011, 01:13 PM
  2. Help with while loop inside a switch case
    By Shesaid in forum New To Java
    Replies: 2
    Last Post: 04-01-2011, 03:36 AM
  3. Loop error
    By Spyderpig in forum New To Java
    Replies: 4
    Last Post: 02-19-2011, 12:56 AM
  4. For loop error
    By ShotGunRockets in forum New To Java
    Replies: 6
    Last Post: 04-06-2009, 01:14 AM
  5. help error in while loop
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-03-2009, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •