Home > Back-end >  How to make this calculator to only accept numbers in first two input's and loop till they are
How to make this calculator to only accept numbers in first two input's and loop till they are

Time:11-12

Somehow I got operator looping till I get correct input. When i try to put num1 or num2 in "if" statement, It says that I cannot convert "int" to "boolean". Please help

public class main {

public static void main(String[] args) {
    int num1;
    int num2;
    String operator;
    
    Scanner scan = new Scanner(System.in);  
    System.out.print("tell me first number: ");
        num1 = scan.nextInt();               //<--input only numbers, loop if not   
    System.out.print("tell me second number: ");
        num2 = scan.nextInt();               //<--input only numbers, loop if not
//////////////////operator////////////////////////
    System.out.print("tell me operator: ");
        operator = scan.next();
            while(true) {   
                if(operator.equals(" ")) {
                    System.out.println("answer is: "  (num1   num2));
                    break;
                }
                else if(operator.equals("-")) {
                    System.out.println("answer is: "  (num1 - num2));
                    break;
                }   
                else if(operator.equals("*")) {
                    System.out.println("answer is: "  (num1 * num2));
                    break;
                }
                else if(operator.equals("/")) {
                    System.out.println("answer is: "  (num1 / num2));
                    break;
                }
                else {
                    System.out.print("wrong input! try again!: ");
                    operator = scan.next();
                }   
            }
    }
}

CodePudding user response:

Try this.

    System.out.print("tell me first number: ");
    while (!scan.hasNextInt()) scan.next();
    num1 = scan.nextInt();
    System.out.print("tell me second number: ");
    while (!scan.hasNextInt()) scan.next();
    num2 = scan.nextInt();

CodePudding user response:

    System.out.print("tell me first number: ");
    while (!scan.hasNextInt()) scan.next();
    num1 = scan.nextInt();
    System.out.print("tell me second number: ");
    while (!scan.hasNextInt()) scan.next();
    num2 = scan.nextInt();

With this, wrong answer was looping till i got correct input, but I could not get it to print me "Wrong Input! Try again!: " with wrong input without starting to loop infinitely, so i tried to edit and came up with this.

//////////////////first number//////////////////// 
      System.out.print("tell me first number: ");
          while(!scan.hasNextInt()) { 
              System.out.print("Wrong Input! Try again!: "); scan.next();         
                  if(scan.hasNextInt() == true) {
              }
          }   
              num1 = scan.nextInt();
  //////////////////second number///////////////////  
      System.out.print("tell me second number: ");
          while(!scan.hasNextInt()) {
              System.out.print("Wrong input! Try again!: "); scan.next();
                  if(scan.hasNextInt() == true) {
              }
          }
              num2 = scan.nextInt();

Somehow it helped lol. Thanks @英語は苦手 for help.

  • Related