Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Advice on booleans and yes/no inputs that deliver specific outputs

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Advice on booleans and yes/no inputs that deliver specific outputs

    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.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Advice on booleans and yes/no inputs that deliver specific outputs

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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)
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

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

  6. The Following User Says Thank You to pbrockway2 For This Useful Post:

    LeaAnn0223 (January 2nd, 2013)

  7. #6
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Advice on booleans and yes/no inputs that deliver specific outputs

    Quote Originally Posted by Norm View Post
    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)
    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

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Advice on booleans and yes/no inputs that deliver specific outputs

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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"
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    LeaAnn0223 (January 2nd, 2013)

  12. #10
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Why do I get 0 in the outputs?
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2012, 12:00 PM
  2. Connecting Inputs to Outputs – Electronic Board
    By podge_y2k1 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 1st, 2012, 09:22 AM
  3. How do you define things from specific keyboard inputs?
    By JavaN00b101 in forum Java Theory & Questions
    Replies: 2
    Last Post: December 1st, 2011, 12:30 AM
  4. Program is attempting to change booleans yet the result is unsuccesful.
    By CoolGuy134 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 08:48 AM
  5. Need some general and specific advice.
    By Morevan in forum Loops & Control Statements
    Replies: 2
    Last Post: January 3rd, 2010, 11:31 PM