# Thread: Problems with a calculator and variables

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

## 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){
}
else{
System.out.println("Would you like to minus the numbers? (yes/no)");
tnum2 = calcu.hasNextBoolean();
if (tnum2=true ){
}
else{
System.out.println("Would you like to times the numbers? (yes/no)");
tnum3 = calcu.hasNextBoolean();
if (tnum3=true ){
}
else{
System.out.println("Would you like to times the numbers? (yes/no)");
tnum4 = calcu.hasNextBoolean();
if (tnum4=true ){

}
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 :)  Reply With Quote

2. Nub Join Date
Sep 2014
Location
Houston
Posts
8
Rep Power
0

## 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.  Reply With Quote

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

## Re: Problems with a calculator and variables Originally Posted by Hamenopi 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 ;_;  Reply With Quote

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

## 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.  Reply With Quote

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

## 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){
}
else{
System.out.println("Would you like to minus the numbers? (yes/no)");
tnum2 = calcu.hasNextBoolean();
if (tnum2=true ){
}
else{
System.out.println("Would you like to times the numbers? (yes/no)");
tnum3 = calcu.hasNextBoolean();
if (tnum3=true ){
}
else{
System.out.println("Would you like to divide the numbers? (yes/no)");
tnum4 = calcu.hasNextBoolean();
if (tnum4=true ){

}
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.  Reply With Quote

6. ## 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.  Reply With Quote

7. Member Join Date
Feb 2014
Posts
7
Rep Power
0

## 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  Reply With Quote

8. Member Join Date
Feb 2014
Posts
7
Rep Power
0

## 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
}```  Reply With Quote

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

## Re: Problems with a calculator and variables Originally Posted by Nybbler 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 :/  Reply With Quote

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

## 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.  Reply With Quote

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

## Re: Problems with a calculator and variables Originally Posted by gimbal2 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.  Reply With Quote

12. ## 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.  Reply With Quote

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

## Re: Problems with a calculator and variables Originally Posted by Norm 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.  Reply With Quote

14. ## Re: Problems with a calculator and variables

Look at the String class's equals() method. It does a compare and returns true or false.  Reply With Quote

#### Posting Permissions

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