Results 1 to 4 of 4
- 07-20-2011, 07:03 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
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: " + "%"); } } }
- 07-20-2011, 07:09 AM #2
Dude
Its because you have entered empty string from keyboard.. Make sure what string you have typed by SOP.Mak
(Living @ Virtual World)
- 07-20-2011, 07:13 AM #3
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.
- 07-20-2011, 07:13 AM #4
Similar Threads
-
junit test case error
By acmohan in forum New To JavaReplies: 7Last Post: 07-12-2011, 01:13 PM -
Help with while loop inside a switch case
By Shesaid in forum New To JavaReplies: 2Last Post: 04-01-2011, 03:36 AM -
Loop error
By Spyderpig in forum New To JavaReplies: 4Last Post: 02-19-2011, 12:56 AM -
For loop error
By ShotGunRockets in forum New To JavaReplies: 6Last Post: 04-06-2009, 01:14 AM -
help error in while loop
By iPetey in forum New To JavaReplies: 3Last Post: 04-03-2009, 03:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks