Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: In need of help with switch-statement code.

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default In need of help with switch-statement code.

    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,407
    My Mood
    Mellow
    Thanks
    143
    Thanked 603 Times in 590 Posts

    Default Re: In need of help with switch-statement code.

    The program asks for a 'letter grade', presumably a, b, c, d, or f, yet the scanner is expecting an integer. That's why there is an input mismatch exception.

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 03:19 PM
  2. Replies: 9
    Last Post: February 24th, 2013, 05:51 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 3rd, 2013, 11:21 PM
  4. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM