# [Help] Basic math program help. A easy to understand thread.

• 01-06-2013, 05:17 PM
MagicalPeppers
[Help] Basic math program help. A easy to understand thread.
Hi people, I am new to Java and I have been learning by way of books. Right now I am learning about loops. So to test my knowledge I decided to make a simple calculator. This calculator has no gui, as I don't know how to make them, but the code and logic should be easy to follow.

Code:

```import java.util.Scanner; public class main {         public static void main(String[] args) {                                 int a;                 int b;                 String calcType;                                 // get input for var int a                 System.out.println("Enter your first number:");                 Scanner input1 = new Scanner(System.in);                 a = input1.nextInt();                 System.out.println("You entered " + a);                                 //get the math operator to be use in the calculation - var string "calcType"                 System.out.println("Now choose your operator. (/*-+)");                 Scanner input2 = new Scanner(System.in);                 calcType = input2.next();                 System.out.println("You entered " + calcType);                                 //get the input for var int b                 System.out.println("Now choose your last number.");                 Scanner input3 = new Scanner(System.in);                 b = input3.nextInt();                 System.out.println("You entered " + b);                                 // Begin calculations!                 //This code figures out the math operator to be used and then does the appropriate calculation.                 if(calcType == "/") {                         double answer1 = a / b;                         System.out.println("The answer is: " + answer1);                 } else if(calcType == "*") {                         int answer2 = a * b;                         System.out.println("The answer is: " + answer2);                 } else if(calcType == "-") {                         int answer3 = a - b;                         System.out.println("The answer is: " + answer3);                 } else if(calcType == "+") {                         int answer4 = a + b;                         System.out.println("The answer is: " + answer4);                 } else {                         // Return if the calculation could not be completed.                         System.out.println("Houston we have a problem!");                 }         } }```
Compiler Error: None
Works Correctly: No
Returns: Houston we have a problem!

I believe the problem is happening in the "Begin calculations section." I am trying to compare the string the user entered (eg /*-+) with a || operator to a typed out version of the entered operator.

This is probably very simple to all of you and I appreciate your time in reading my thread and helping me make this code work. Please tell me what is done wrong.

Thank you! :D:
• 01-06-2013, 05:25 PM
JosAH
Re: [Help] Basic math program help. A easy to understand thread.
Don't compare objects (such as Strings) for equality wth the == operator; use the .equals( ... ) method instead, e.g. s.equals("+").

kind regards,

Jos
• 01-06-2013, 05:32 PM
MagicalPeppers
Re: [Help] Basic math program help. A easy to understand thread.
Quote:

Don't compare objects (such as Strings) for equality wth the == operator; use the .equals( ... ) method instead, e.g. s.equals("+").

kind regards,

Jos

Thank you Jos for the quick response! I really appreciate you pointing out the correct syntax for me. And best of all I have learned something, which is awesome!

The new IF statement bock of code (works perfect):

Code:

```// Begin calculations!                 //This code figures out the math operator to be used and then does the appropriate calculation.                 if(calcType.equals("/")) {                         double answer1 = a / b;                         System.out.println("The answer is: " + answer1);                 } else if(calcType.equals("*")) {                         int answer2 = a * b;                         System.out.println("The answer is: " + answer2);                 } else if(calcType.equals("-")) {                         int answer3 = a - b;                         System.out.println("The answer is: " + answer3);                 } else if(calcType.equals("+")) {                         int answer4 = a + b;                         System.out.println("The answer is: " + answer4);                 } else {                         // Return if the calculation could not be completed.                         System.out.println("Houston we have a problem!");                 }```

Thanks again! you rock! :(y):