Results 1 to 20 of 27
- 08-04-2012, 05:33 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Need help with + - * / calculator
I'm new to java, and trying to make just a basic calculator, I want the calculator to be able to cycle through options till the desired math function is found, then used. It doesn't seem to work so help would be appreciated.
This is my code so far:
Java Code:import java.util.Scanner; public class calculator { public static void main(String args[]) { Scanner input = new Scanner(System.in); double fnum, snum, answer; String name = input.nextLine(); System.out.println("Do you want to add?"); name = input.nextLine(); if (name == "yes") { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum + snum; System.out.println(answer); } else { System.out.println("okay"); } System.out.println("Do you want to subtract?"); name = input.nextLine(); if (name == "yes") { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum - snum; System.out.println(answer); } else { System.out.println("okay"); } System.out.println("Do you want to multiply?"); name = input.nextLine(); if (name == "yes") { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum * snum; System.out.println(answer); } else { System.out.println("okay"); } System.out.println("Do you want to divide?"); name = input.nextLine(); if (name == "yes") { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum / snum; System.out.println(answer); } else { System.out.println("okay"); } } }
- 08-04-2012, 05:39 PM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
When comparing strings for equality, you want to use this:
The == operator should only be used to check if primitive data types are equal. When applied to objects, it checks if the two references point to the same object.Java Code:String1.equals(String2)
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 05:40 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
So where would I put that in my code? (Sorry I'm new to all of this)
-
Re: Need help with + - * / calculator
Whenever I see something like this -- a desire to cycle through code -- I automatically think of using a loop of some sort, here a while loop where you keep looping until the user gives you a valid reply.
You'll want to give us as detailed a description of what it is doing or not doing as it will greatly help us help you.It doesn't seem to work so help would be appreciated.
An immediate big issue I see on quick scan of your code is that you're comparing Strings using ==. Don't do that is this checks if one String variable refers to the exact same object as another String variable (or literal) which is not what you really want to do. Instead you want to make sure that the two Strings contain the same characters in the same order, and so you'll want instead to use either the equals(...) or equalsIgnoreCase(...) methods.This is my code so far:
Java Code:import java.util.Scanner; public class calculator { public static void main(String args[]) { Scanner input = new Scanner(System.in); double fnum, snum, answer; String name = input.nextLine(); System.out.println("Do you want to add?"); name = input.nextLine(); if (name == "yes") { // ******
- 08-04-2012, 05:44 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
I understand, but still have no idea how to change my code... :(
- 08-04-2012, 05:49 PM #6
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
Re: Need help with + - * / calculator
pretty sure you need to put the string inside the curly braces bro, might be wrong though. Can someone confirm this
else {
System.out.println("okay");
}
System.out.println("Do you want to subtract?");
name = input.nextLine();
if (name == "yes") {
System.out.println("Please enter first number:");
fnum = input.nextDouble();
System.out.println("Please enter second number:");
snum = input.nextDouble();
answer = fnum - snum;
System.out.println(answer);
}
- 08-04-2012, 05:51 PM #7
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
Do you know how to use a while loop? If not, learn here: While Loops
As for fixing your comparison of Strings, you would just have to replace your current boolean conditions involving the == operator with boolean conditions using the .equals() method."Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 05:52 PM #8
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
Re: Need help with + - * / calculator
this might help bro, Bucky is so helpful!
Java - 7 - Building a Basic Calculator
- 08-04-2012, 05:54 PM #9
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
I have done this
But I don't know how to take it so it satisfies as a yes or no answerif (name.equalsIgnoreCase(name)) {
- 08-04-2012, 05:56 PM #10
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
okay I solved it by changing it to:
My last problem is I have to type something in the console to start the program, how can I make the program start automatically?if (name.equalsIgnoreCase("Yes")) {
- 08-04-2012, 05:59 PM #11
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
Firstly, you don't want to check if the content of variable name is equal to the content of variable name because this will always be true. You want to check if the content of variable name is equal to "yes".
Secondly, if the boolean expression evaluates to true, then that means that the person entered "yes". If the boolean expression evaluates to false, then the person entered something other than yes (which in this case means "no"). This boolean expression provides you with a yes/no question."Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
-
Re: Need help with + - * / calculator
Please use [code] [/code] tags not quote tags around your posted code in this forum.
As for your last statement what exactly do you mean? How will the program magically start if you don't tell the operating system to run it? I think you need to type in "java your program name" for it to run.
- 08-04-2012, 06:03 PM #13
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
I mean, to run in eclipse's console I have start the program which is fine, but then type something to start the code sequence, is there a way to start the code without doing that?
- 08-04-2012, 06:05 PM #14
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 06:07 PM #15
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
No, because that is what defines the name variable, without that line it doesn't work.
- 08-04-2012, 06:09 PM #16
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
Where else can I initialize it?
- 08-04-2012, 06:09 PM #17
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
Yes, I caught that and edited my previous post. You must initialize it somewhere else. In fact, you can just change line 7 to this:
Java Code:String name;
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 06:11 PM #18
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
Thank you, for god sake another problem! The answer counts as an answer for the next statement. Any ideas?
- 08-04-2012, 06:13 PM #19
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
Just to clarify, I used some wrong terminology. When I said initialize, I meant to say define. Initializing a variable involves actually assigning a value to a variable.
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 06:15 PM #20
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Similar Threads
-
calculator help
By sirstroud in forum New To JavaReplies: 0Last Post: 04-01-2012, 05:58 AM -
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 -
Calculator
By water in forum AWT / SwingReplies: 4Last Post: 09-23-2009, 06:00 AM -
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks