Advice on booleans and yes/no inputs that deliver specific outputs
First, let me start off by saying I'm a total noob. So this padawan is asking for patience.
So, I'm working with a simple code that expands a bit on the "Hello World" code.
What I WANT it to do is say hello and ask for the users name. Then I ask if the user is okay today.
Then if the user responds with a Yes ...I want it to println a certain response.
If the user responds with a No....I want it to println with a different response.
I have gotten as far as the +input of the user's name....that's all good.
what I'm struggling with is, and I'm assuming it would be a boolean is either true=yes and false=no responses.
I have googled countless booleans and each one I've found deals with variable expressions and it's not exactly what I'm looking for and I think it's just confusing me.
I've tried manipulating an if/else but well, I wouldn't be here asking for assistance if it worked.
So, I'm not looking for spoon feeding...just a tip or a point in the right direction.
if the code worked properly the out put would be:
This is a test:
Please enter your name:
Confused Padawan
Hello, Confused Padawan, are you well?
(if yes println would be something like)
Fantastic, go buy yourself a new speeder.
(if no, printlin would be something like)
That's too bad. Go buy a new lightsaber and kill something.
Thanks for your assistance :)
Re: Advice on booleans and yes/no inputs that deliver specific outputs
Quote:
I have gotten as far as the +input of the user's name....that's all good.
what I'm struggling with is, and I'm assuming it would be a boolean is either true=yes and false=no responses.
I have googled countless booleans and each one I've found deals with variable expressions and it's not exactly what I'm looking for
Post the code so far - ie asking for the name, assigning it to a variable and using it to print the response. Almost certainly the next step will involve a boolean expression. (In fact it must, because that's what booleans are: they are the type of ... expressions). But post the code so far.
Re: Advice on booleans and yes/no inputs that deliver specific outputs
Code Java:
import java.util.Scanner;
class Test{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
Scanner myScanner = new Scanner (System.in);
String input;
System.out.println("This is a test");
System.out.println("Please enter your name");
input = myScanner.nextLine();
System.out.println("Hello, " + input + ", are you well?" );
input = myScanner.nextLine();
if ( a )
{
System.out.println("Fantastic, go buy yourself a speeder" );
}
if ( b )
{
System.out.println("That's too bad. Go buy a lightsaber and kill" +
"something");
}
}
}
Re: Advice on booleans and yes/no inputs that deliver specific outputs
The values of a and b must be set by some assignment statement: (a & b are poor choices for variable names. They have no meaning. Use a name that represents the value they would hold. For example: atEndOfJob)
Code :
boolean xIsGreater = x > y;
boolean itsJohn = name.equals("John");
Then the boolean can be used later in the if statements like you have coded.
When using booleans in an if statement there can only be one of two results: true or false.
Use the else statement with the if for the false condition. Don't use a separate if statement.
Re: Advice on booleans and yes/no inputs that deliver specific outputs
Quote:
The values of a and b must be set by some assignment statement
To unpack Norm's comment a bit, you have a String variable input and you have just assigned the user's answer to it ("yes", "no", etc). What you have to do now is assign the right thing to what you are calling a. (I agree it's a bad name. If a has the value "true" what does that mean? I'm not asking because I want you to tell me, I'm asking because the answer is what you should rename the variable to.)
Since input is a String look in your textbook, notes etc for a useful looking method of the String class that returns a boolean. The most comprehensive list of such methods is the String API documentation. It's long and you aren't supposed to remember all the methods (that's why they're listed on a web page) but as you search for something that can test whether the user entered "yes" you will get a feel for all the other sorts of behaviour that strings have.
Do you see any useful looking methods?
Re: Advice on booleans and yes/no inputs that deliver specific outputs
Quote:
Originally Posted by
Norm
The values of a and b must be set by some assignment statement: (a & b are poor choices for variable names. They have no meaning. Use a name that represents the value they would hold. For example: atEndOfJob)
Code :
boolean xIsGreater = x > y;
boolean itsJohn = name.equals("John");
Then the boolean can be used later in the if statements like you have coded.
When using booleans in an if statement there can only be one of two results: true or false.
Use the else statement with the if for the false condition. Don't use a separate if statement.
Thank you for your response. So I've had a play around with it again and I'm wondering if you might be able to explain that in another way, please. And just for clarity sake, why are a & b poor choices for variable names?
--- Update ---
Thanks....I didn't see this until I got the refresh on the post. Thank you for your responses.
--- Update ---
so, basically I'm just not getting how to initialize the boolean is what you're saying lol
Re: Advice on booleans and yes/no inputs that deliver specific outputs
Quote:
I'm just not getting how to initialize the boolean
I gave two examples of how to set boolean variables using a boolean expression. Did you understand how those statements work?
What variable do you want to set?
With what conditions do you want it to have a true value? Or false value?
Re: Advice on booleans and yes/no inputs that deliver specific outputs
you gave good examples....I'm just reaffirming that I must go back and refresh on how to set the boolean variable.
this code is suppose to interact with the user. The code asks if the users is "well". "if" they type in the box a "yes"..... response 1 is implemented or sent to the user..... "else" the user types in 'no" then response 2 is sent to the user.
so I suppose, or at least in my mind, the yes response would generate the "true" response.....anything else typed in would generate a false response and thus, by default, generate the 2nd response. Does that make sense?
Re: Advice on booleans and yes/no inputs that deliver specific outputs
Look at how the itsJohn variable is set. It compares a variable's contents against a String constant.
For your problem, the variable would be what the user entered and the String constant: "yes"
Re: Advice on booleans and yes/no inputs that deliver specific outputs
Thanks for your help...that has cleared it up for me....now to go do it, again and again and again AND again! lol