-4

I am a beginner to programming (literally two days new), and I am having an issue. I am trying to make a VERY basic calculator, where the user inputs the first number, the operation (only +, -, *, and /), and then the second number. I'm having trouble getting the input of the operation to determine what the answer will be with the if statements, so can someone please explain it using very simple terminology?

package learn;
import java.util.Scanner;

class calculator{
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
    double termOne, operation, termTwo, answer;
    System.out.println("Enter first term: ");
    termOne = input.nextDouble();
    System.out.println("Enter operation (Valid operations are : ");
    operation = input.nextDouble();
    System.out.println("Etner second term: ");
    termTwo = input.nextDouble();

    if (operation == add) {
        answer = termOne + termTwo;
        System.out.println(answer);
    } else if (operation == subtract) {
        answer = termOne - termTwo;
        System.out.println(answer);
    } else if (operation == divide) {
        answer = termOne / termTwo;
        System.out.println(answer);
    } else (operation == multiply) {
        answer = termOne * termTwo;
        System.out.println(answer);
    }
}
}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • 3
    Where are `add` and its friends defined...? – Tunaki Mar 05 '16 at 21:06
  • 2
    It looks like you are asking for string representing operation, but you are reading double which is kind of confusing. Also if you will end up with reading String don't compare it using `==`. Read: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Mar 05 '16 at 21:07
  • @Pshemo [In the JDK 7 release, you can use a String object in the `public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay; switch (dayOfWeekArg) { case "Monday": typeOfDay = "Start of work week"; break; case "Tuesday": case "Wednesday":`](https://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.html) – Valentin Tihomirov Mar 05 '16 at 21:10
  • @ValentinTihomirov True, I am not saying we can't, I was just trying to point out that we shouldn't compare strings with `==`. – Pshemo Mar 05 '16 at 21:12
  • 1
    @Mark It is impossible to understand which statement you are talking about. Localize your question and, then, you will be able to find the answer yourself. It is called "debugging" amongst professional programmers. – Valentin Tihomirov Mar 05 '16 at 21:13

1 Answers1

0

Okay so both Tunaki and Pshemo are right but I will try to point out your problems on a more simplified level.

Your first mistake is the way you declare your variable "operation". You use the datatype double (which is for floating point numbers, kinda) but later want it to hold a string.

Secondly, I'm assuming you are checking if the user typed in "add" to then add the two numbers. However there are several problems here:

  1. You are not using quotes for your string so Java is looking for a variable called add rather than the actual "add".

  2. String comparison in java does (mostly) not work this way. A better way to compare two strings is to use String.equals("otherString"). In your example that would be if(operation.equals("add"))

I hope this helps... Oh and I welcome anyone to format my post, I'm on a mobile and it's a pain in the a**

FMaz
  • 384
  • 2
  • 14