Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By MagicalPeppers

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

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

    Question [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
    		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!
    Last edited by MagicalPeppers; 01-06-2013 at 06:19 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: [Help] Basic math program help. A easy to understand thread.

    Quote Originally Posted by JosAH View Post
    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;
    			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!
    Last edited by MagicalPeppers; 01-06-2013 at 06:39 PM.
    Fubarable likes this.

Similar Threads

  1. Help me write an easy program?
    By esined93 in forum New To Java
    Replies: 1
    Last Post: 10-17-2012, 04:22 PM
  2. Can you help me understand this basic GUI concept?
    By EscSequenceAlpha in forum New To Java
    Replies: 1
    Last Post: 04-08-2012, 09:33 PM
  3. Basic Thread Help
    By armyson in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-28-2011, 09:56 AM
  4. Another easy program I should be able to do
    By Goff256 in forum New To Java
    Replies: 12
    Last Post: 04-16-2011, 08:40 PM
  5. How do I perform basic math with BigDecimals
    By s0meb0dy in forum New To Java
    Replies: 4
    Last Post: 09-26-2010, 01:32 PM

Posting Permissions

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