-3

I was given a simple assignment to accomplish, creating a grading scale that would give a student a letter grade for a value that was submitted. Everything seems to run smoothly except after the student is done - they need to enter "E" to exit the program... This is where I am lost because I receive this exception "Exception in thread "main" java.util.InputMismatchException". I know the outcome is something small that I am missing, but I cant figure it out here is the code that I currently have:

public static void main(String[] args) {

    int letterGrade;//The overall letter grade
    boolean prompt = true;
    while (prompt) {
        //Prompt the student to input their data
        System.out.print("Please enter your exam score, or press E to exit :");

        @SuppressWarnings("resource")
        Scanner keyboard = new Scanner(System.in);
        letterGrade = keyboard.nextInt();

        if ((letterGrade >= 90) & (letterGrade <= 100)) {
            System.out.println("The letter grade is A");
        } else if ((letterGrade >= 80) & (letterGrade <= 89)) {
            System.out.println("The letter grade is B");
        } else if ((letterGrade >= 70) & (letterGrade <= 79)) {
            System.out.println("The letter grade is C");
        } else if ((letterGrade >= 60) & (letterGrade <= 69)) {
            System.out.println("The letter grade is D");
        } else if ((letterGrade >= 0) & (letterGrade <= 59)) {
            System.out.println("The letter grade is F");
        } else {
            System.out.println("Invalid input, try again.");
        }
    }

    String firstChar = "e";
    String secondChar = "E";

    {
        if ((firstChar == secondChar)) {
            System.out.println("Thank you for using the grading system");
        } else {
            System.out.println("Thank you for using the grading system");
        }

        {

        }

    }

    {

    }
}
}

WHAT am I missing? I've been at this for two weeks?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • 2
    "E" is not an `int`. – Robby Cornelissen Sep 30 '14 at 01:00
  • How could I receive letter grades and exit with program with "E" when I wasn't using [int] the letter grades weren't matching up with the values being input. – cup_of_coffee Sep 30 '14 at 01:05
  • Also `firstChar == secondChar` doesn't do what you think it does... – John3136 Sep 30 '14 at 01:10
  • 1
    In addition to @Bruno's answer, note that you're using bitwise `&` operators here when it appears what you want is `&&` - this will lead to some unexpected results if you continue this practice. – drew moore Sep 30 '14 at 01:12
  • Good point @drewmoore, didn't see it – Bruno Franco Sep 30 '14 at 01:15
  • Get input from keyboard by "String keyIn = keyboard.nextLine();" and check first character of keyIn if "E" or not. If not "E", convert string to integer for using letterGrade. – Fumu 7 Sep 30 '14 at 01:16
  • Thanks @Bruno, to make your answer a little more complete (and +1 worthy :), I'd suggest making a warning about that, and also about his string comparison in `((firstChar == secondChar))` – drew moore Sep 30 '14 at 01:17
  • learn to format your code consistently, if you want free help you have to make it as easy for someone to help you as possible. Every major IDE has a built in code formatter, learn its hotkey and use it, early and often! –  Sep 30 '14 at 01:24
  • also learn to search the site for questions that already have answers! –  Sep 30 '14 at 01:26
  • op s problem is not because wrong string compare.yes it's wrong the way op compares Strings.but the main problem is taking every input as int without checking int or not because "e" is possible to input.that exception occur when e entered – Madhawa Priyashantha Sep 30 '14 at 01:37
  • "Exception in thread "main" java.util.InputMismatchException" doesn't occur because wrong string comparison – Madhawa Priyashantha Sep 30 '14 at 01:41

1 Answers1

1

main problem is taking every input as int without checking int or not because "e" is possible to input. if you want user to input "E" or "e" you should first check is user input value numeric or not.you can use try{catch} but good to use regex.and comparing strings not correct in your code.you can use || to check user input e or E but .tolowercase or equalsIgnoreCase is simple

public class NewClass {

public static void main(String[] args) {

    String firstChar = "e";
    String secondChar = "E";
    boolean prompt = true;
    while (prompt) {
        //Prompt the student to input their data
        System.out.print("Please enter your exam score, or press E to exit :");

        @SuppressWarnings("resource")
        Scanner keyboard = new Scanner(System.in);
        String userinput = keyboard.next();
        if(isNumeric(userinput)){
            int letterGrade=Integer.parseInt(userinput);
            if ((letterGrade >= 90) & (letterGrade <= 100)) {
                System.out.println("The letter grade is A");
            } else if ((letterGrade >= 80) & (letterGrade <= 89)) {
                System.out.println("The letter grade is B");
            } else if ((letterGrade >= 70) & (letterGrade <= 79)) {
                System.out.println("The letter grade is C");
            } else if ((letterGrade >= 60) & (letterGrade <= 69)) {
                System.out.println("The letter grade is D");
            } else if ((letterGrade >= 0) & (letterGrade <= 59)) {
                System.out.println("The letter grade is F");
            } else {
                System.out.println("Invalid input, try again.");
            }
        }else if(userinput.equalsIgnoreCase(firstChar)){
             System.out.println("Thank you for using the grading system");
             System.exit(0);
        }
    }

}

    public static boolean isNumeric(String str) {
      return str.matches("-?\\d+(\\.\\d+)?"); 
    }
}

and if you are not allowed to use method equalsignorecase you can use or operator

else if(userinput.equals(firstChar)||userinput.equals(secondChar)){
      System.out.println("Thank you for using the grading system");
      System.exit(0);
}

and also if you are not allowed to use regex .replace isNumeric method with following try catch logic

public static boolean isNumeric(String str)  
{  
  try  
  {  
    double d = Double.parseDouble(str);  
  }  
  catch(NumberFormatException ex)  
  {  
    return false;  
  }  
  return true;  
}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • learn to use `.equalsIgnoreCase()` is more efficient and the idiomatic way to test equality without case sensitivity! It also works with `i18n` correctly! –  Sep 30 '14 at 01:27
  • @JarrodRoberson ok .i changed tolowercase to equalsIgnoreCase – Madhawa Priyashantha Sep 30 '14 at 01:31
  • @JarrodRoberson main problem is taking every input as int without checking int or not because "e" is possible to input.this is correct approach for this problem.i understand equalsIgnoreCase definitely effective in this case. – Madhawa Priyashantha Sep 30 '14 at 01:56