I am running into error after error with this program. At this point, things seem to be in order for the most part and I can compile without errors, however when I run the program and I'm asked to enter a letter grade, entering any letter grade will give me this:
Exception in thread "main" java.util.InputMisMatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at GPA.main(GPA.java:28)
Any help is greatly appreciated.. Excuse my noobness, I am brand new to Java (in my 3rd day of programming) but very determined.
import java.util.Scanner; public class GPA
{
public static void main(String[] args)
{
Scanner kbReader = new Scanner(System.in);
System.out.println("Enter number of classes: ");
int numberClasses = kbReader.nextInt();
int i;
int totalCreditForGPA = 0;
int totalCreditHours = 0;
for (i = 0; i < numberClasses; i++)
{
System.out.print("Enter letter grade of class: ");
int letterGrade = kbReader.nextInt();
System.out.print("Enter credit hour of class: ");
int creditHour = kbReader.nextInt();
switch (letterGrade)
{
case 'A':
case 'a': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 4);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'B':
case 'b': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 3);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'C':
case 'c': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 2);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'D':
case 'd': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 1);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'F':
case 'f': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 0);
totalCreditHours = totalCreditHours + creditHour;
break;
default:
System.out.println("Invalid letter grade.");
}
}
int GPA = totalCreditForGPA;
System.out.println("Your GPA is " + GPA);
}
}
My apologies for crappy formatting/indenting... I don't know how to copy and paste over to this forum and keep formatting.