Click here to Skip to main content
15,890,408 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've recently started to try to learn java and today I tried to make a calculator. When I run the code and say to add, subtract, multiply, or divide, it always comes out with "That is not a correct operator". Please try to be specific... here is the code (I use the netbeans IDE).

Calculator.java

Java
package calculator;
import java.util.Scanner;

public class Calculator {
    
    
    
    public static void main(String[] args) {
        
        String operator;
        
        System.out.println("Would you like to add, subtract, multiply, or divide");
        Scanner choiceOP = new Scanner(System.in);
        String choice = choiceOP.nextLine();
        
        //for a testing purpose... delete after completion
        System.out.println(choice);
        
        methods method = new methods();
        
        if (choice == "add"){
            operator = "add";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.add(num1, num2);
        } else if (choice == "subtract"){
            operator = "subtract";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.subtract(num1, num2);
        } else if (choice == "multiply"){
            operator = "multiply";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.multiply(num1, num2);
        } else if (choice == "divide"){
            operator = "divide";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.divide(num1, num2);
        } else{
            System.out.println("That is not a correct operator");
        }
        
    }
}


Methods.java

Java
package calculator;

public class methods {
    
    public void add(int num1, int num2){
        double answer = num1 + num2;
        System.out.println("The answer is " + answer);
    }
    public void subtract(int num1, int num2){
        double answer = num1 - num2;
        System.out.println("The answer is " + answer);
    }
    public void multiply(int num1, int num2){
        double answer = num1 * num2;
        System.out.println("The answer is " + answer);
    }
    public void divide(int num1, int num2){
        double answer = num1 / num2;
        System.out.println("The answer is " + answer);
    }
    
}


Thanks in advance!!

What I have tried:

I have tried everything I could including looking it up.
Posted
Updated 2-Sep-16 15:30pm

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

The value of choice is not recognized as an expected operator.
Use the debugger to run the program line by line and see the value of choice as the program run.
 
Share this answer
 
well, its been some time since I did java, but Im pretty sure of a few things

a) Netbeans allows you to step through your code (debug) - trust me, its a good skill to learn, and allows you to see what your variables contain (for example)

b) if your code is all the way at the end, then, do you not think that inspecting this

Scanner choiceOP = new Scanner(System.in);
String choice = choiceOP.nextLine();

//for a testing purpose... delete after completion
System.out.println(choice);


is worthwhile - does the System.out.println actually display add or subtract etc on the console .. if it does, then

c) you might need to review string comparison in java

ie
choice == "add"
.. are you sure thats how to compare strings ? while its 'Legal' ie the compiler allows you to do it, its actually not doing what you want - so, some reading, look at Java: ==, .equals(), compareTo(), and compare()[^] for example, and Im pretty sure you should grasp that

if (choice.equals( "add")){
is probably what you want - try it on just one of the comparisons as a start to prove it
 
Share this answer
 
Comments
Ghostdragon777 3-Sep-16 8:16am    
Thx for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900