Results 1 to 14 of 14
Like Tree3Likes
  • 2 Post By Hamenopi
  • 1 Post By gimbal2

Thread: Problems with a calculator and variables

  1. #1
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Problems with a calculator and variables

    Okay, so I know how to get user input and create a variable from the input, create a basic addition sum ect ect.
    So I have this code:
    package calc;
    import java.util.Scanner;
    class calc{
    private static final double add = 0;
    private static final double subtract = 0;

    public static void main(String args[]){
    Scanner calcu = new Scanner(System.in);
    double fnum, snum, answer;

    boolean tnum2, tnum3, tnum4, tnum;
    System.out.println("Enter first number");
    fnum = calcu.nextDouble();
    System.out.println("Enter second number");
    snum = calcu.nextDouble();
    System.out.println("Would you like to add the number? (yes/no)");
    tnum = calcu.hasNextBoolean();


    if (tnum=true){
    answer = fnum+snum;
    }
    else{
    System.out.println("Would you like to minus the numbers? (yes/no)");
    tnum2 = calcu.hasNextBoolean();
    if (tnum2=true ){
    answer = fnum-snum;
    System.out.println(answer);
    }
    else{
    System.out.println("Would you like to times the numbers? (yes/no)");
    tnum3 = calcu.hasNextBoolean();
    if (tnum3=true ){
    answer = fnum*snum;
    System.out.println(answer);
    }
    else{
    System.out.println("Would you like to times the numbers? (yes/no)");
    tnum4 = calcu.hasNextBoolean();
    if (tnum4=true ){
    answer = fnum/snum;
    System.out.println(answer);


    }
    else{
    System.out.println("Sorry, I don't know what you want to do");
    }
    }

    }
    }



    }
    }
    I'm basically trying to make it so that when the user enters tnum, tnum2, tnum3 and tnum4, their answer turns to the variable which has to be either true or false to make the boolean work. I don't know really how to do this. I want to make it so that if they enter yes, then that makes the boolean true and the numbers will multiply and create the answer variable. If they enter no then the boolean is false and it moves onto the next if statement. How can I do this? It'd be really helpful if someone could edit the code so that it works and then just tell me what you've done.
    Btw really enjoying programming in java I love the language so far :)

  2. #2
    Hamenopi is offline Nub
    Join Date
    Sep 2014
    Location
    Houston
    Posts
    8
    Rep Power
    0

    Default Re: Problems with a calculator and variables

    Hai!
    I was wondering if you would be so kind as to wrap your code in code brackets.
    Presently, it kinda makes my eyes bleed and that makes me sad.
    gimbal2 and DarrylBurke like this.

  3. #3
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Problems with a calculator and variables

    Quote Originally Posted by Hamenopi View Post
    Hai!
    I was wondering if you would be so kind as to wrap your code in code brackets.
    Presently, it kinda makes my eyes bleed and that makes me sad.
    I literally just got onto this forum how do I do that ;_;

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Problems with a calculator and variables

    like in most (tech) forums, you do that by putting the code in between [code][/code] tags.

    Documentation:

    http://www.vbulletin.org/forum/misc.php?do=bbcode
    Last edited by gimbal2; 09-16-2014 at 09:49 PM.
    Hamenopi likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Problems with a calculator and variables

    thanks :) I hope you understand what I'm trying to achieve here.
    Java Code:
    package calc;
    import java.util.Scanner;
    class calc{
    	private static final double add = 0;
    	private static final double subtract = 0;
    
    	public static void main(String args[]){
    		Scanner calcu = new Scanner(System.in);
    		double fnum, snum, answer;
    		
    		boolean tnum2, tnum3, tnum4, tnum;
    		System.out.println("Enter first number");
    		fnum = calcu.nextDouble();
    		System.out.println("Enter second number");
    		snum = calcu.nextDouble();
    		System.out.println("Would you like to add the number? (yes/no)");
    		tnum = calcu.hasNextBoolean();
    		
    		
    		if (tnum=true){
    			answer = fnum+snum;
    		}
    			else{
    				System.out.println("Would you like to minus the numbers? (yes/no)");
    				tnum2 = calcu.hasNextBoolean();
    				if (tnum2=true ){
    					answer = fnum-snum;
    					System.out.println(answer);
    				}
    				else{
    					System.out.println("Would you like to times the numbers? (yes/no)"); 
    					tnum3 = calcu.hasNextBoolean();
    					if (tnum3=true ){
    						answer = fnum*snum;
    						System.out.println(answer);
    					}
    					else{
    						System.out.println("Would you like to divide the numbers? (yes/no)");
    						tnum4 = calcu.hasNextBoolean();
    						if (tnum4=true ){
    							answer = fnum/snum;
    							System.out.println(answer);
    							
    									
    						}
    						else{
    							System.out.println("Sorry, I don't know what you want to do");
    						}
    					}
    				
    			}
    		}
    		
    			
    	
    	}
    }
    Last edited by Francisbojczuk; 09-16-2014 at 09:56 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problems with a calculator and variables

    I hope you understand what I'm trying to achieve here.
    It often helps other people understand code by adding comments to the code that explains what it is supposed to be doing and how it is going to do it.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Nybbler is offline Member
    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Default Re: Problems with a calculator and variables

    On your Scanner object "calcu" you are calling "hasNextBoolean" which returns true if the value entered can be interpreted as a boolean. I think you want to use "nextBoolean" instead. However, I don't know if a "yes" or "no" answer would be interpreted as a boolean by the Scanner object.

    Okay, I just tested it and no, it will only accept true or false as a valid value. You could read the input as a String then use the String's "equals()" method to compare it to "yes" or "no" and set a boolean that indicates what was entered.
    Last edited by Nybbler; 09-16-2014 at 10:44 PM. Reason: additional info

  8. #8
    Nybbler is offline Member
    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Default Re: Problems with a calculator and variables

    I just noticed another issue. You are checking in your if statements if a value is equal to true. You are using a single equals which is actually an assignment operater. You meant to use "==". However, since you are checking a boolean value, you don't even have to do that. You can for example write:

    Java Code:
    if (tnum){
        //code here
    }

  9. #9
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Problems with a calculator and variables

    Quote Originally Posted by Nybbler View Post
    I just noticed another issue. You are checking in your if statements if a value is equal to true. You are using a single equals which is actually an assignment operater. You meant to use "==". However, since you are checking a boolean value, you don't even have to do that. You can for example write:

    Java Code:
    if (tnum){
        //code here
    }
    That's pretty interesting.
    I don't know the code though which would actually depend on a user input. So say if the user entered "yes" in tnum2, it would be stored in the variable, however, I don't know how to use the user's input as a command. I find it hard to articulate myself, sorry, but I mean if the user entered yes then it would execute the minus-ing, however, if the user entered no it would carry on with the code, if he entered anything else it would maybe re-ask the question.
    I've kinda hit a wall with my limited knowledge on java :/

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Problems with a calculator and variables

    If I understand you correctly, I can rephrase it as:

    if what the user inputs equals "yes", then I do thingsAndStuffA(). If what he enters equals "no" then I do thingsAndStuffB(). Otherwise the code loops back to the beginning.

    If I phrase it like that, do you perhaps get an idea how to proceed?

    And ps: "yes" and "no" are NOT boolean values to Java. Only the values "true" and "false" are.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Thumbs up Re: Problems with a calculator and variables

    Quote Originally Posted by gimbal2 View Post
    If I understand you correctly, I can rephrase it as:

    if what the user inputs equals "yes", then I do thingsAndStuffA(). If what he enters equals "no" then I do thingsAndStuffB(). Otherwise the code loops back to the beginning.

    If I phrase it like that, do you perhaps get an idea how to proceed?

    And ps: "yes" and "no" are NOT boolean values to Java. Only the values "true" and "false" are.
    YES YES!! Except, it doesn't go back to the begining, it prints another line with the another question e.g. "Would you like to times the numbers yes/no". and then I guess you could repeat the code except slightly changes because this time I want to times it or minus it etc etc.
    I just want the user's input to change what happens next depending on what they type (yes or no). I mean sure I can store numbers but I can't figure out how to make it so that the users text actually changes what the code does... If you get me.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problems with a calculator and variables

    how to make it so that the users text actually changes what the code does
    Use an if statement that compares what the user entered against some constants like: "yes" or "no" using some of the String class's methods.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Problems with a calculator and variables

    Quote Originally Posted by Norm View Post
    Use an if statement that compares what the user entered against some constants like: "yes" or "no" using some of the String class's methods.
    I don't know any code which does that. I'm a noob, sorry.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problems with a calculator and variables

    Look at the String class's equals() method. It does a compare and returns true or false.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Problems with calculator program
    By Madhatter in forum New To Java
    Replies: 8
    Last Post: 12-10-2013, 04:14 AM
  2. need some help with my calculator
    By Tooks1 in forum New To Java
    Replies: 0
    Last Post: 02-13-2013, 06:38 PM
  3. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  4. Replies: 6
    Last Post: 11-03-2010, 02:33 PM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 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
  •