0

I am new to programming and would appreciate some help. The slightest bit of insight would be highly appreciated. I have an issue with the following code. The program emulates a calculator but currently my main focus is on if and else if statements. The issue is that no matter what the user selects, the program will always add the two numbers i.e. 'number1' and 'number2' in the code

import java.util.*;
public class Input
{
    private Scanner input;

    public Input()
    { 
        input = new Scanner(System.in);

    }

    public void calculation()
    {
        double number1, number2, answer;
        String A, B, C, D, E;
        String option;
        A = "A"; B = "B"; C = "C"; D = "D"; E = "E"; //initialising the strings 

        System.out.println("add - option A \t (if your option is A, insert 'A')");
        System.out.println("multiply - option B");
        System.out.println("subtract - option C");
        System.out.println("divide - option D");
        System.out.println("power - option E (1st number - 'X' & 2nd number - 'n' following X^n)");
        System.out.println("Remember Java is case sensitive, therefore, inserting 'a' as 'A' won't work");
        System.out.println();
        System.out.println("Insert your first number: ");
        number1 = input.nextDouble();
        System.out.println("Insert your second number: ");
        number2 = input.nextDouble();
        System.out.println("Choosing option: ");
        option = input.next();

        if(A == A)
        {
            answer = number1 + number2;
            System.out.println("Your answer is: " + answer);
        }

        else if(B == B)
        {
            answer = number1 * number2;
            System.out.println("Your answer is: " + answer);

        }else if(C == C)
        {
            answer = number1 - number2;
            System.out.println("Your answer is: " + answer);

        }else if(D == D)
        {
            answer = number1 / number2;
            System.out.println("Your answer is: " + answer);

        }else if(E == E)
        {
            answer = Math.pow(number1, number2);
            System.out.println("Your answer is: " + answer);

        }else
        {
            System.out.println("Choose a suitable option");

        }    
    }
}
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
evonzz
  • 139
  • 6
  • 7
    Any object is always `==` to itself. – rgettman Nov 15 '13 at 21:59
  • 2
    Did you mean `A == "A"`? That would be wrong too. – Sotirios Delimanolis Nov 15 '13 at 21:59
  • Don't compare strings with `==`, instead use `string.equals(...)` – Robert H Nov 15 '13 at 21:59
  • You probably mean `option == A` and so on.. – karthikr Nov 15 '13 at 22:00
  • @karthikr In which case it would still be `.equals()`. – Steve P. Nov 15 '13 at 22:00
  • Your `if` statement appears to be working just fine. `A == A` is always `true` and, as the `if` statement should, it executes the code in that block. By the way, you can use `String` expressions in `switch` statements these days. – Jason C Nov 15 '13 at 22:00
  • @SotiriosDelimanolis This is not a duplicate of that question (as `A.equals(A)` vs. `A == A` is not the root problem right now), although the OP's *next* question after he fixes his general logic errors will most likely be a duplicate of that question. – Jason C Nov 15 '13 at 22:02
  • @JasonC Are they not implicitly asking how to compare Strings? – Sotirios Delimanolis Nov 15 '13 at 22:04
  • @RobertH so how would the 'string.equals' look? I assume i put it within the circle brackets within the if statements and then insert one of the fields i have defined? – evonzz Nov 15 '13 at 22:04
  • @SotiriosDelimanolis Well, I personally don't think so. The way I see it, the OP is having a more fundamental problem conceptualizing the logic of his program, mostly as evidenced by `A==A`, `B==B`, instead of an attempt at `option==A`, `option==B`. So there seems to be a deeper issue with his thought process that could be addressed in answers here but is not addressed by a general question regarding string comparison. As it stands, `A.equals(A)` and `B.equals(B)` would not solve his problem alone. It's up for interpretation. Once the OP gets the logic down, *then* the string question is next. – Jason C Nov 15 '13 at 22:06
  • 1
    @JasonC My initial attempt was with option==A and so on but whenever i run the program it jumps straight to the else statement – evonzz Nov 15 '13 at 22:09
  • Well in that case, your problem most certainly was "how do I compare strings?" and @SotiriosDelimanolis duplicate link contains the information you would have needed. – Jason C Nov 15 '13 at 22:14
  • 1
    @JasonC I'm just busting balls :P – Sotirios Delimanolis Nov 15 '13 at 22:20

1 Answers1

7

Youre getting the selected option from user input in option = input.next(); line and than your not using it in your if statements.

Instead of if(A == A) use if(option.equals(A)) and so on for other cases.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162