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

1. Member Join Date
Jan 2013
Posts
4
Rep Power
0

## [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.

Java 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
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"
Scanner input2 = new Scanner(System.in);
calcType = input2.next();
System.out.println("You entered " + calcType);

//get the input for var int b
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;
} else if(calcType == "*") {
int answer2 = a * b;
} else if(calcType == "-") {
int answer3 = a - b;
} else if(calcType == "+") {
int answer4 = a + b;
} 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! Last edited by MagicalPeppers; 01-06-2013 at 05:19 PM.  Reply With Quote

2. ## 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  Reply With Quote

3. Member Join Date
Jan 2013
Posts
4
Rep Power
0

## Re: [Help] Basic math program help. A easy to understand thread. Originally Posted by JosAH 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):

Java 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;
} else if(calcType.equals("*")) {
int answer2 = a * b;
} else if(calcType.equals("-")) {
int answer3 = a - b;
} else if(calcType.equals("+")) {
int answer4 = a + b;
} else {
// Return if the calculation could not be completed.
System.out.println("Houston we have a problem!");
}```

Thanks again! you rock! Last edited by MagicalPeppers; 01-06-2013 at 05:39 PM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•