Search code examples
javaloopsintegerruntime-error

How do I make this program check if an input is an integer without causing an error in runtime and stop the loop with a sentinel value?


I've been trying to root out a runtime error from my program caused while it is checking if user input is an integer, and if it isn't one to print out an error message, and ask for another input that is an integer. I've also noticed that with checking the integer now my program keeps asking for more input instead of stopping the loop if a user inputs a negative integer. Here's the part of my program code that is having issues:

do
    {
        System.out.println("Enter an integer grade percentage: ");
        examScore = keyboard.nextInt();
        if (examScore != (int)examScore) {
            System.out.println("Not an integer, please try again.");            
                
            if (examScore >= 90 && examScore <=100) {
                gradeA++;
                totalScores++;
            }
            else if (examScore >= 80 && examScore <=89) {
                gradeB++;
                totalScores++;
            }
            else if (examScore >= 70 && examScore <=79) {
                gradeC++;
                totalScores++;
            }
            else if (examScore >= 60 && examScore <=69) {
                gradeD++;
                totalScores++;
            }
            else if (examScore >=0 && examScore <= 59)
                gradeF++;
                totalScores++; 
            }
        else {
            if (examScore >= 90 && examScore <=100) {
                gradeA++;
                totalScores++;
            }
            else if (examScore >= 80 && examScore <=89) {
                gradeB++;
                totalScores++;
            }
            else if (examScore >= 70 && examScore <=79) {
                gradeC++;
                totalScores++;
            }
            else if (examScore >= 60 && examScore <=69) {
                gradeD++;
                totalScores++;
            }
            else if (examScore >= 0 && examScore <=59)
                gradeF++;
                totalScores++;           
            }
        }while(examScore >=0);

All help is appreciated!


Solution

  • You're missing an opening { on the last else if.

    nextInt() returns an int so what is the point of if (examScore != (int)examScore) {?

    You can use Scanner.hasNextInt() to check if user has inputted an integer. If not, consume the input and try again.

    You can simplify the grade conditionals by taking advantage of the fact that in an if/else if/else, once one block is executed the rest are skipped.

    while (true) {
        System.out.println("Enter an integer grade percentage: ");
        do {
            // Check for integer
            while (!keyboard.hasNextInt()) {
                // Not an int? Consume and discard
                String input = keyboard.next();
                System.out.println("Not an integer, please try again.");
            }
            examScore = keyboard.nextInt();
        } while (examScore > 100); // Only except if in range
        if (examScore >= 90) {
            gradeA++;
        }
        else if (examScore >= 80) {
            gradeB++;
        }
        else if (examScore >= 70) {
            gradeC++;
        }
        else if (examScore >= 60) {
            gradeD++;
        }
        else if (examScore >= 0) {
            gradeF++;
        }
        else {
            // Must be negative
            break;
        }
        totalScores++; 
    }