# [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):
{"ft_lang_label":"__label__en","ft_lang_prob":0.7832183,"math_prob":0.9397017,"size":3518,"snap":"2020-24-2020-29","text_gpt3_token_len":860,"char_repetition_ratio":0.16334662,"word_repetition_ratio":0.36759582,"special_character_ratio":0.29022172,"punctuation_ratio":0.21805556,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99313945,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-14T00:31:06Z\",\"WARC-Record-ID\":\"<urn:uuid:1b02354f-b7da-4080-ba4d-a9680091a13a>\",\"Content-Length\":\"13189\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8330f9aa-8fc7-4619-98d1-c9659d81c9aa>\",\"WARC-Concurrent-To\":\"<urn:uuid:6d7978ec-e533-443e-b9d9-3cf0cd6b2ff6>\",\"WARC-IP-Address\":\"163.172.65.71\",\"WARC-Target-URI\":\"https://www.java-forums.org/new-java/67349-help-basic-math-program-help-easy-understand-thread-print.html\",\"WARC-Payload-Digest\":\"sha1:RTUBBTUBKZ23UKX2IE6ITY3SK2JJGDGY\",\"WARC-Block-Digest\":\"sha1:DRGTFPB6C4PW5GIFYELCI63PR7436GZM\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593657147031.78_warc_CC-MAIN-20200713225620-20200714015620-00133.warc.gz\"}"}