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

Thread: What to return after in this boolean method with the if/else statement?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default What to return after in this boolean method with the if/else statement?

    What do I have to return after the the if/else statement in my boolean method?

    public class Hoofd {
        public static boolean getName(String x)
        {
            if(x == "jennifer")
                return true;
            else if(x == "ruurs")
                return false;
            return //what do i have to write here?;
     
        }
     
     
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What to return after in this boolean method with the if/else statement?

    The method's name says it will return a name, not a boolean. If the method is supposed to check for a valid name then its name needs to be changed to show that.
    For example:
    public static boolean checkValidName(String name)
    Since a boolean has only two values, the method returns either true or false. The test for "ruurs" is redundant if there is only one case where the method will return true.

    The code should use the equals method to test the contents of a String object, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: What to return after in this boolean method with the if/else statement?

    Smart suggestions!

Similar Threads

  1. [SOLVED] Boolean return missing
    By Time4Java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2014, 03:36 AM
  2. Please help with method return statement!
    By brobertson300 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 3rd, 2014, 07:10 AM
  3. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 11:02 AM
  4. [SOLVED] Boolean return print
    By Gerardgrundy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 4th, 2012, 05:38 AM
  5. 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