Results 1 to 6 of 6
Thread: Calculator error?
- 09-02-2012, 07:47 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
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:
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: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); } } }
It doesn't give any error or something, but it just doesn't print the answer. Anyone knows what I did wrong?Java Code:Add the first digit 3 Plus(+) ,min(-) ,multiply(*) or divide(/)? - Add the second digit 1
Also, feel free to post any other java tips if you have them :).
- 09-02-2012, 07:51 PM #2
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.
- 09-02-2012, 07:55 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
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 08:04 PM.
- 09-04-2012, 12:16 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
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 :)
- 09-04-2012, 02:44 PM #5
Re: Calculator error?
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-05-2012, 12:12 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
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
-
RMD Calculator
By java software in forum Java SoftwareReplies: 0Last Post: 10-08-2011, 05:42 PM -
calculator
By rithish in forum AWT / SwingReplies: 10Last Post: 04-25-2011, 11:24 PM -
Help with AWT CALCULATOR
By Megan Dosnueve in forum AWT / SwingReplies: 2Last Post: 04-04-2011, 05:49 PM -
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
[SOLVED] Simple Calculator Applet Weird Error
By sari in forum New To JavaReplies: 5Last Post: 01-28-2009, 04:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks