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 4 of 4

Thread: Boolean return print

  1. #1
    Junior Member Gerardgrundy's Avatar
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Boolean return print

    Hi,
    I'm trying to do a boolean method

    public boolean x()
       {
          if (y() >= z)
          {
             return true;
          }
          else
          {
             return false;
          }
       }

    though I'm unsure how to send the print data out to:
    I need it to print out YES if TRUE and NO if false.
    How would I call the boolean x() method and test it for true and flase and print it?

    public void f()
       {
          System.out.printf( "\nc:" + c );
    //where would I write print in the boolean method? I thought these methods are just for returning true or false

    Thanks for your help in advance


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Boolean return print

    Is the method supposed to return a boolean or a String? (true/false) or ("YES"/"NO")
    Is the method supposed to print something or return something to be printed? (edit) ...or both?

  3. #3
    Junior Member Gerardgrundy's Avatar
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Boolean return print

    The method has to return true false.
    Then that is to be checked and if true print Yes if false no.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Boolean return print

    Use the return value of method x() for the conditional in an if statement like you did in the body of the x() method. If x() returns true print one thing, else print something else.
    It sure seems like this boolean method is unnecessary.
    For example instead of the entire body of the x() method you could use the following line:
    return y() >= z;
    because the statement:
    y() >= z
    will evaluate to a boolean value and you can simply return it rather than saying if(true) { return true; } else { return false; }
    That being the case you could replace the call to x() in the code with y() >= z and forget the method x() completely.
    Anyway too many possibilities, and I am rambling at the wee hours of my morning. Someone will be happy to look over what you have if you want to post it.

  5. The Following User Says Thank You to jps For This Useful Post:

    Gerardgrundy (November 4th, 2012)

Similar Threads

  1. [SOLVED] Boolean or If?
    By 0ffConstantly in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2012, 11:32 AM
  2. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  3. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM