-2

I am new to java, i am trying to make a little calculator that takes the input of two numbers and an operator. This data is then taken into an if statement to calculate the answer. Then output into the console. I am having a problem though because with the:

operator = Calc.next();

Because i thought that the above line of code was correct for inputting a string but it doesn't work, whatever the numbers input to the calculator are, the answer seems to be output as zero.

Please take the code from the pastebin link and look at it because i'm not sure what is going wrong. I would appreciate someone letting me know where i went wrong.

http://pastebin.com/B5PjkXwX

Thanks

  • 5
    paste your code here, please – Andrew Tobilko Mar 09 '16 at 19:25
  • 1
    and please show the stack trace also. – Cory Roy Mar 09 '16 at 19:27
  • 1
    Welcome to StackOverflow. Please visit the [help] and also read [ask] to understand the guidelines for posting here. You are expected to post a sufficient example of your code to explain the problem (preferably a [mcve]), along with any error messages and/or stack trace, and a description of what you've done so far to troubleshoot. – Jim Garrison Mar 09 '16 at 19:27
  • 3
    Already voted to close, but this is also a duplicate: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – David Mar 09 '16 at 19:28
  • You are comparing strings using == instead of equals (`operator == "+"`). Use equals (`"+".equals(operator)`). – Pablo Mar 09 '16 at 19:29

2 Answers2

1

Strings compare through equals(String other), not through the == operator.

if (operator.equals("+")) {
    answer = firstNumber + secondNumber;
} 
...

Also, I advise you to turn the if-else statements into the switch statement.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0
import java.util.Arrays;
import java.util.Scanner;

public class Hello {

    public static void main(String args[]) {

        double firstNumber;
        double secondNumber;
        double answer = 0;
        String operator;

        Scanner Calc = new Scanner(System.in);

        System.out.println("\\\\CALCULATOR\\\\");

        System.out.println("Enter first number");
        firstNumber = Calc.nextDouble();

        System.out.println("Enter the math operator to use for this calculation");
        operator = Calc.next();

        System.out.println("Enter second number");
        secondNumber = Calc.nextDouble();

        if (operator.equals("+")) {
            answer = firstNumber + secondNumber;
        } else if (operator.equals("-")) {
            answer = firstNumber - secondNumber;
        } else if (operator.equals("/")) {
            answer = firstNumber / secondNumber;
        } else if (operator.equals("*")) {
            answer = firstNumber * secondNumber;
        }

        System.out.println(answer);
    }

}

or use a switch as shown below

import java.util.Arrays;

import java.util.Arrays;
import java.util.Scanner;

public class Hello {

    public static void main(String args[]) {

        double firstNumber;
        double secondNumber;
        double answer = 0;
        String operator;

        Scanner Calc = new Scanner(System.in);

        System.out.println("\\\\CALCULATOR\\\\");

        System.out.println("Enter first number");
        firstNumber = Calc.nextDouble();

        System.out.println("Enter the math operator to use for this calculation");
        operator = Calc.next();

        System.out.println("Enter second number");
        secondNumber = Calc.nextDouble();

        switch (operator) {
        case "+":
            answer = firstNumber + secondNumber;
            break;
        case "-":
            answer = firstNumber - secondNumber;
            break;
        case "/":
            answer = firstNumber / secondNumber;
            break;
        case "*":
            answer = firstNumber * secondNumber;
            break;
        default:
            System.out.println("Incorrect operator");
            break;
        }

        System.out.println(answer);
    }

}
Monroy
  • 420
  • 5
  • 19