0

im only 15 and new to java so i am trying to build a simple calculator, but i cant seem to figure out why this if statement is being ignored. I have check to be sure that all values are being stored and yes they are so i can not see any other problems which would explain this. Any help would be great! Look for the comment in the second class //This if statement

The first class

public class CalculatorOperations {

    double fnum, snum,answer;
    String operation;

    void plus(){

        operation="+";
        answer = fnum + snum;
    }

    void subtract(){

        operation="-";
        answer = fnum - snum;
    }

    void multiple(){

        operation="*";
        answer = fnum * snum;
    }

    void divide(){

        operation="/";
        answer = fnum / snum;
    }

    void invalidOperation(){

        System.out.println("Invalid operation.");
    }

    void showAttributes(){
        System.out.println(fnum);
        System.out.println(snum);
        System.out.println(operation);
    }
}

The second class

    import java.util.Scanner;

public class calculatorApplication {

    public static void main(String [] args){

        CalculatorOperations Operators = new CalculatorOperations();
        Scanner userInput = new Scanner(System.in);

        String loop2 = null;
        boolean loop;

         while (loop = true){

            // Getting input and storing it
                System.out.print("Please enter first number: ");
                Operators.fnum = userInput.nextDouble();

                System.out.println("TEST:"+Operators.fnum);

                System.out.print("Please enter second number: ");
                Operators.snum = userInput.nextDouble();

                System.out.println("TEST:"+Operators.snum);

                System.out.print("Please enter operation (+, -, * or /): ");
                Operators.operation = userInput.next();

                System.out.println("TEST:"+Operators.operation);

                // this if statement

                if (Operators.operation == "+") {
                    Operators.plus();

                } else if (Operators.operation == "-") {
                    Operators.subtract();

                } else if (Operators.operation == "*") {
                    Operators.multiple();

                } else if (Operators.operation == "/") {
                    Operators.divide();

                } else {

                    Operators.invalidOperation();
                }



                System.out.println("Answer: " +Operators.answer);

                System.out.print("Would you like to do another sum? (yes or no): ");
                loop2 = userInput.next();


         }

         if (loop2.equals("yes") || loop2.equals("Yes")){
             loop = true;
                System.out.println();
                System.out.println();
            }else{
                loop = false;
                  // Closes scanner to prevent resource leaks 
                userInput.close();
                System.exit(0);
            }


                }

    }
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97

3 Answers3

2

Comparing Strings with == generally doesn't work the way you'd like it to. It's because Strings are Objects and == compares object references against each other, instead of checking if the Strings contain identical text.

Try String.equals instead:

if (Operators.operation.equals("+")) {
... //and of course the same for the rest of the statements

Good luck with your program!

kviiri
  • 3,282
  • 1
  • 21
  • 30
  • Thank You working great now!!! Is there any others ways i could improve my coding? I would love to learn java, just the idea of starting with nothing and coming out with a fully working program! –  Nov 03 '13 at 17:34
  • You should learn about concept beyond a language such as: OOP, design patterns, architectural patterns, algorithm techniques, being in SO ;). A language it's only a medium to make those ideas real. In general one idea/sentence/problem triggers a whole new world to investigate. – Matthew Azkimov Nov 03 '13 at 17:39
  • You'll realise how bad your code is/was if you just keep learning. It'll improve quite a lot. Make sure to read http://docs.oracle.com/javase/tutorial/java/. – Troubleshoot Nov 03 '13 at 17:44
0

Use the .equals(String) method, instead of ==. Your if-structure would change to this:

if (Operators.operation.equals("+")) { Operators.plus();

} else if (Operators.operation.equals("-")) {
    Operators.subtract();

} else if (Operators.operation.equals("*")) {
    Operators.multiple();

} else if (Operators.operation.equals("/")) {
    Operators.divide();

} else {
    Operators.invalidOperation();
}

.equals(String) is used for comparing strings, whereas == is used for comparing everything else pretty much. == is comparing the reference to an object and .equals(String) is used to compare String values.

Also, change while (loop = true) to while(loop) or while (loop == true); otherwise you are indicating that you are actually changing the value of loop.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0

You don't want to compare strings with == because by doing that you're comparing the reference of the string, and not the value of the string. You need to use the .equals method.

if (Operators.operation.equals("+"))

From the javadoc:

boolean equals(Object anObject)

Returns true if and only if the argument is a String object that represents the same sequence of characters as this object.

Also, you need to change

while (loop = true)

to

while (loop)

= is the assignment operator, == is the comparison operator.

Troubleshoot
  • 1,816
  • 1
  • 12
  • 19