Results 1 to 6 of 6
  1. #1
    RexBox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Unhappy Calculator error?

    I'm new to Java, but I really like learning it and all :).

    But, I was trying to make an working calculator, so I wrote this code:


    Java Code:
    import java.util.Scanner;
    
    
    public class Calculator {
    	public static void main(String lol[]){
    		System.out.println("Add the first digit");
    	Scanner InsertDigit1 = new Scanner(System.in);
    	double digit1 = InsertDigit1.nextInt();
    	System.out.println("Plus(+) ,min(-) ,multiply(*) or divide(/)?");
    	Scanner InsertMath = new Scanner(System.in);
    	String Math = InsertMath.nextLine();
    	System.out.println("Add the second digit");
    	Scanner InsertDigit2 = new Scanner(System.in);
    	double digit2 = InsertDigit2.nextInt();
    	
    	// || means "or", right?
    
    	if (Math == "+" || Math == "Plus")
    			
    	{
    		double Awnser = (digit1 + digit2);
    		System.out.println(Awnser);
    	}
    	
    	else if (Math == "-" || Math == "Min")
    		
    	{
    		double Awnser = (digit1 - digit2);
    		System.out.println(Awnser);
    	}
    	else if (Math == "*" || Math == "Multiply")
    		
    	{
    		double Awnser = (digit1 * digit2);
    		System.out.println(Awnser);
    	}
    	else if (Math == "/" || Math == "Divide")
    		
    	{
    		double Awnser = (digit1 / digit2);
    		System.out.println(Awnser);
    	}
    	}
    }
    That was my code. Everything works untill I come to the last part, where it should give the answer. This is what I get:


    Java Code:
    Add the first digit
    3
    Plus(+) ,min(-) ,multiply(*) or divide(/)?
    -
    Add the second digit
    1
    It doesn't give any error or something, but it just doesn't print the answer. Anyone knows what I did wrong?

    Also, feel free to post any other java tips if you have them :).

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Calculator error?

    You should use the equals() method when comparing String objects, not the == operator.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    RexBox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Calculator error?

    How do I use it? Sorry, but like I said I'm new :(

    Edit: Nevermind, found the solution!
    Last edited by RexBox; 09-02-2012 at 09:04 PM.

  4. #4
    hardcoder is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Calculator error?

    If you undersatand the basic difference it will be easy for you "==" only compares the references of the string, in simple language, it will got to the address in the memory and search where these two strings are if the address is same then it will return true . Here toy have created two different references so address must be different so it failing. Instead "equals" method will compare the actual content in the string, gift from String class that it is overridden there :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Calculator error?

    Quote Originally Posted by hardcoder View Post
    If you undersatand the basic difference it will be easy for you "==" only compares the references of the string, in simple language, it will got to the address in the memory and search where these two strings are if the address is same then it will return true . Here toy have created two different references so address must be different so it failing. Instead "equals" method will compare the actual content in the string, gift from String class that it is overridden there :)
    Vary badly expressed. The == comparison operator compares identity equality; it tests whether two primitives have the same value, or whether two references have the same value. By definition, two Java references have the same value if they refer to the same object OR if both are null.

    Nothing to do with memory addresses, which are taken care of by the JVM -- not by the Java language.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    hardcoder is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Calculator error?

    I dont know why everyone here is directly explaining the functionalities, let the people get the content firs, they are telling they are new, they want to learn!! let them go in deep, I will suggest to read java docs, read about object class its fields, methods..... Primitives can be compared when == is just an operator but here it beyond that... please try to explain in depth which will be helpful beyond this particular doubt!!

Similar Threads

  1. RMD Calculator
    By java software in forum Java Software
    Replies: 0
    Last Post: 10-08-2011, 06:42 PM
  2. calculator
    By rithish in forum AWT / Swing
    Replies: 10
    Last Post: 04-26-2011, 12:24 AM
  3. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 06:49 PM
  4. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 09:21 PM
  5. [SOLVED] Simple Calculator Applet Weird Error
    By sari in forum New To Java
    Replies: 5
    Last Post: 01-28-2009, 05:57 AM

Posting Permissions

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