Вопрос

I am getting this as an error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "q"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at exercises.GPACalculator.main(GPACalculator.java:53)

public static void main(String[] args) 
{
            String input = "";
    String letterGrade = "";
    String creditHour = "";
    int credits = 0;
    int points = 0;
    int course = 1;
    int gpa;
    String grade = "";

    String greeting = "Hello, this program will calculate your GPA. You will be asked \n"+
            "to enter your letter grade for each class, then you will be asked to enter \n"+
            "the corresponding number of credits for that class. Once all the grades and \n"+
            "credits have been entered, the program will display your GPA.";
    JOptionPane.showMessageDialog(null,greeting,"Greeting - Introduction",1);

    String gradePrompt = "Enter your letter grade (A, B, C, D, F)\n"+
            "Press \"Q\" to quit and display your results";
    String creditPrompt = "Enter the credit hours for course "+course+" (0, 1, 2, 3, 4, 5, 6)\n\n"+
            "Press \"Q\" to quit and display your results";

    do      
    {
        input = JOptionPane.showInputDialog(null,gradePrompt,"Enter grade",1);
        letterGrade += input.toUpperCase();

        input = JOptionPane.showInputDialog(null,creditPrompt,"Enter credit hours",1);
        creditHour += input.toUpperCase();
        course++;
        if(input.toUpperCase().equals("Q"))
            continue;
            credits = Integer.parseInt(input);

            switch (grade) {
            case "A":  points = 4;
                break;
            case "B":  points = 3;
                break;
            case "C":  points = 2;
                break;
            case "D":  points = 1;
                break;
            case "F":  points = 0;
                break;
            }

        //input = JOptionPane.showInputDialog(null,"Do you want to quit? (Press Q "+
        //      "to quit, no to continue)" ,"Do you want to quit?",1);
    }while(!input.toUpperCase().equals("Q"));

    input = input.substring(0,input.length()-1);

    gpa = points / credits;

    String results = "The courses you entered are:\n\n"+
            "Grade  "+"Hours    \n\n"+
            letterGrade+"   \n\n"+creditHour+"\n\n"+
            "Resulting in a GPA of "+gpa+"\n\n"+
            "This program will now terminate!";

    JOptionPane.showMessageDialog(null, new JTextArea(results),
            "GPA results",1);
}

}

Это было полезно?

Решение 2

As the stacktrace shows it is not able to convert a string to integer here :

credits = Integer.parseInt(input);

Your while loop checks if input has value 'Q'. But the problem is that you update the value of input inside the while code itself. So when you provide value Q the loop will complete the current iteration and break while checking for the next iteration. I suggest you to put another input at the end of the loop.

input = JOptionPane.showInputDialog(null,"Do you want to quit? Press Q" ,"Do you want to quit? Press Q ",1);

Другие советы

Problem is in this part

String creditPrompt = "Enter the credit hours for your course (0, 1, 2, 3, 4, 5, 6)\n"+
            "Enter Q to display your results\n\n";    
input = JOptionPane.showInputDialog(null,creditPrompt,"Enter grade",1);
credits = Integer.parseInt(input);

You are trying to parse "Q" to int. You should first check that input is not "Q" and then parse the input to get the credits

At

credits = Integer.parseInt(input);

You are converting the input to an integer. If the input is "Q" which is not a number, NumberFormatException will be thrown. Either you need to give your input with a number or handle NumberFormatException in your code.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top