James B
James B

Reputation: 231

Validating mathematical operators in simple calculator

I need help validating operators in a calculator program, the user is asked for two numbers and an operator i.e. 3 + 2 but I cannot validate the operator. If the user enters a +, -, *, or / operator it tells me this input is invalid when it should be fine. Validating the first number is fine there's no problem there. Can someone please help?

import java.util.Scanner;
public class SimpleCalculator {

    public static void main(String[] args) {
        int int1;
        String operator;
        int int2;

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter first number: ");

        while(!scan.hasNextInt()){
            scan.next();
            System.err.print("Invalid input please enter a number: ");
        }

        int1 = scan.nextInt();

        System.out.println("Enter operator (+, -, *, /): ");

        while(!scan.equals("+") && !scan.equals("-") && !scan.equals("*") && !scan.equals("/")){
                scan.next();
            System.err.print("Invalid input please enter a valid operator (+, -, *, /");
        }

        operator = scan.next();

        System.out.println("Enter second number: ");


        scan.close();
    }

}

Upvotes: 0

Views: 1416

Answers (5)

Redtama
Redtama

Reputation: 1613

Your mistake is here:

while(!scan.equals("+") && !scan.equals("-") && !scan.equals("*") && !scan.equals("/"))

The equals() method performs a comparison between the object calling the method and the object passed into the method as an argument.

For example the statement if (myString.equals(anotherString)), compares the myString object with the anotherString object.

Since scan is the name of your Scanner, when you write scan.equals("+") you are comparing to see if your Scanner is equal to the String "+". Which is impossible!

The easiest solution to understand would be to just put the next String entered by the user into a variable and then check that variable contains an operator.

Example

String operator = scan.next();

while(!operator.equals("+") && !operator.equals("-") 
      && !operator.equals("*") && !operator.equals("/")) {   
    operator = scan.next();
    System.err.print("Invalid input please enter a valid operator (+, -, *, /");
}

The other answers will help you to improve your code more than this. I just thought I would show you the smallest change you could make to your code to fix your specific problem. :)

Upvotes: 0

Rohith
Rohith

Reputation: 73

while(!scan.hasNext("[*+/-]")){

use this code instead of while(!scan.equals("+") && !scan.equals("-") && !scan.equals("*") && !scan.equals("/")){

Upvotes: 1

Neil Masson
Neil Masson

Reputation: 2689

You need to check the output of scan.next(), not of scan

    System.out.println("Enter operator (+, -, *, /): ");
    operator = scan.next();
    while(!operator.equals("+") && !operator.equals("-") && !operator.equals("*") && !operator.equals("/")) {
        System.err.println("Invalid input please enter a valid operator (+, -, *, /");
        operator = scan.next();
    }

Upvotes: 1

Andreas
Andreas

Reputation: 159086

Scanner does not implement equals(), so you're doing identity comparisons, and Scanner is never equal to a String literal.

Use this loop instead:

while(!scan.hasNext("[+\\-*/]")){

FYI: Your "skip" calls should be scan.nextLine(), not scan.next(), so all input on the line is ignored.

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30819

I would suggest going with a char type as we are only reading a single letter. This should do:

Scanner reader = new Scanner(System.in);
char c = reader.nextChar();

if(c == '+' || c == '-' || c == '/' || c == '*') {
      //operation
}else{
      //Error message
}

Upvotes: 0

Related Questions