java while loop with if/else statement, else argument runs forever instead of restarting while loop

Viewed 36

So I have an assignment where I am supposed to prompt the user to enter an arithmetic operator, and then 2 numbers, and produce the result; i.e. user enters /, 20, 2 and the OUTPUT should be 10. I am using checks to ensure the proper value types are entered by the user, and my check for the integers entered runs forever if the else statement is activated. I am not sure where I am going wrong here. any guidance would be greatly appreciated.

import java.util.Scanner; public class mainClass {

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    boolean charCheck = true; //for verifying user arithmetic input
    boolean numCheck = true; //for verifying user int input
    boolean sysCheck = true; //for program outer while loop
    int n1 = -1; //user entered number 1
    int n2 = -2; //user entered number 2
    char x = ' '; //user entered arithmetic operator
    while (sysCheck == true)
    {
    while (charCheck == true)
    {
        System.out.println("Please enter either +, -, *, /to proceed. Enter x to end the program");
        x = input.next().charAt(0);

        switch(x) //check input validity for x before the program proceeds
        {
            case '+':
            case '-':
            case '*':
            case '/':    
              System.out.println(x);
              charCheck = false;
              break;
            case 'x':
                System.exit(0);
                break;
            default:
                System.out.print("That is an invalid entry. ");
        }
    }
        System.out.println("Now, please enter 2 numbers, seperated by a space: ");
        while(numCheck)
        {
        if(input.hasNextInt())
        {
        n1 = input.nextInt();
        n2 = input.nextInt();
        System.out.println(n1 + " " + n2);
        numCheck = false;
        }
        else
            System.out.print("That is an invalid entry. ");                
        }
        
        switch(x) //check input validity for x before the program proceeds
        {
            case '+':
                System.out.print("The result is: " + (n1 + n2));
                break;
            case '-':
                System.out.print("The result is: " + (n1 - n2));
                break;
            case '*':
                System.out.print("The result is: " + (n1 * n2));
                break;                   
            case '/':
                System.out.print("The result is: " + (n1 / n2));
                break;
        }


    }
}
}
1 Answers

After playing around with the code more and researching more, I found the solution. the issue I was having was due to not including the line input.next(); in my else statement (input is the name of my scanner, if you named your scanner something else, that name would be in it's place)

        while(numCheck)
        {
        System.out.println("Now, please enter 2 numbers, seperated by a space: ");
        
        if(!input.hasNextInt())
        {
        System.out.println("That is an invalid entry. ");       
        input.next();
        }
        else
        {
        n1 = input.nextInt();
        n2 = input.nextInt();
        numCheck = false;
        }            
        }
        
Related