Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree2Likes

Thread: Need help with + - * / calculator

  1. #1
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default 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");
    		}
    
    	}
    }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    When comparing strings for equality, you want to use this:

    Java Code:
    String1.equals(String2)
    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.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    So where would I put that in my code? (Sorry I'm new to all of this)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with + - * / calculator

    Quote Originally Posted by Aconitum View Post
    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.
    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.

    It doesn't seem to work so help would be appreciated.
    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.

    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") { // ******
    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.
    awinston likes this.

  5. #5
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    I understand, but still have no idea how to change my code... :(

  6. #6
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default 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);
    }

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    Quote Originally Posted by Aconitum View Post
    I understand, but still have no idea how to change my code... :(
    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

  8. #8
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    this might help bro, Bucky is so helpful!

    Java - 7 - Building a Basic Calculator

  9. #9
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    I have done this
    if (name.equalsIgnoreCase(name)) {
    But I don't know how to take it so it satisfies as a yes or no answer

  10. #10
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    okay I solved it by changing it to:
    if (name.equalsIgnoreCase("Yes")) {
    My last problem is I have to type something in the console to start the program, how can I make the program start automatically?

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    Quote Originally Posted by Aconitum View Post
    I have done this
    if(name.equalsIgnoreCase(name)) {
    But I don't know how to take it so it satisfies as a yes or no answer
    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

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with + - * / calculator

    Quote Originally Posted by Aconitum View Post
    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?
    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.

  13. #13
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default 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?

  14. #14
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    Quote Originally Posted by Aconitum View Post
    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?
    On line 7 of your code, you demand input for seemingly no reason. If you remove that line, then the program should automatically ask if you want to add.

    Edit: But you must initialize variable name somewhere else!
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  15. #15
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    No, because that is what defines the name variable, without that line it doesn't work.

  16. #16
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    Where else can I initialize it?

  17. #17
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default 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

  18. #18
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    Thank you, for god sake another problem! The answer counts as an answer for the next statement. Any ideas?

  19. #19
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default 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

  20. #20
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    Quote Originally Posted by Aconitum View Post
    Thank you, for god sake another problem! The answer counts as an answer for the next statement. Any ideas?
    I'm not sure what you mean. Could you clarify?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Page 1 of 2 12 LastLast

Similar Threads

  1. calculator help
    By sirstroud in forum New To Java
    Replies: 0
    Last Post: 04-01-2012, 06:58 AM
  2. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 06:49 PM
  3. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 09:21 PM
  4. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 07:00 AM
  5. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 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
  •