0

I want to do a simple beginner project using methods, if statements, and user input. I am having an issue though with the calc() method. How can I return two different data types in java, and if I cannot, how could I do it, still by using more than the main method?


import java.util.Scanner; //allow user input

public class fourFunctionCalculator{
    public static void main(String[] args) {
        Scanner keyboardInput = new Scanner(System.in);

        System.out.print("Enter your first number:"); //get first number
        double num1 = keyboardInput.nextDouble();

        System.out.print("Enter your operator: ");     // get operator
        String name = keyboardInput.next(); //grabs everything user types until a space

        System.out.print("Enter your second number: ");  //get second number
        double num2 = keyboardInput.nextDouble();

        System.out.println(calc(num1,op,num2));
    }


//troublesome part is here
    public static double calc(double num1, String op, double num2){
        if (op == "+") {
            return (num1 + num2);
        }
        else if (op == "-") {
            return (num1 - num2);
        }
        else if (op == "*") {
            return (num1 * num2);
        }
        else if (op == "/") {
            return (num1 / num2);
        }
        else {
            return ("INVALID OPERATOR");
        }

    }
}
Myspace
  • 63
  • 2
  • 10

3 Answers3

1

you could generate a custom Exception, also you need to use the method .equals() inside the if validations, otherwise it is not going to work.

fourFunctionCalculator.java

import java.util.Scanner; //allow user input

public class fourFunctionCalculator{
    public static void main(String[] args) {
        Scanner keyboardInput = new Scanner(System.in);
        System.out.print("Enter your first number:"); //get first number
        double num1 = keyboardInput.nextDouble();
        System.out.print("Enter your operator: ");     // get operator
        String name = keyboardInput.next(); //grabs everything user types until a space
        System.out.print("Enter your second number: ");  //get second number
        double num2 = keyboardInput.nextDouble();
        try {
            System.out.println(calc(num1,name,num2));
        } catch (InvalidOperatorException e) {
            System.out.println(e);
        }

    }


    public static double calc(double num1, String op, double num2){
        if (op.equals("+")) {
            return (num1 + num2);
        }
        else if (op.equals("-")) {
            return (num1 - num2);
        }
        else if (op.equals("*")) {
            return (num1 * num2);
        }
        else if (op.equals("/")) {enter code here
            return (num1 / num2);
        }
        throw new InvalidOperatorException("INVALID OPERATOR : " + op);
    }
}

InvalidOperatorException.java

public class InvalidOperatorException 
extends RuntimeException {
    private static final long serialVersionUID = 1L;

    public InvalidOperatorException(String errorMessage) {
        super(errorMessage);
    }
}
0

I recommend returning an OptionalDouble object as placeholder of something valid or not...

Ex #1:  return OptionalDouble.of(num1 + num2); // first if outcome

Ex #2:  return OptionalDouble.empty(); // for the last else

Then your main(...) method needs to be something like...

System.out.println(calc(num1,op,num2).orElseThrow(() -> new IllegalArgumentException("INVALID OPERATOR")));
Jeff Stewart
  • 125
  • 5
0
I suggest returning always String.


 public static String calc(double num1, String op, double num2){
            if (op == "+") {
                return String.valueOf(num1 + num2);
            }
            else if (op == "-") {
                return String.valueOf(num1 - num2);
            }
            else if (op == "*") {
                return String.valueOf(num1 * num2);
            }
            else if (op == "/") {
                return String.valueOf(num1 / num2);
            }
            else {
                return ("INVALID OPERATOR");
            }

        }
Shivaraja HN
  • 168
  • 1
  • 10