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

Thread: If and else if statements???

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default If and else if statements???

    hey so I'm relatively new to java (2 weeks or so) and I have latched onto the idea of if and else if statements. I made this program to output the details of an animal, which include name, height, how many legs it has and whether or not it has a tail. For the last statement, I tried using an if statement to make it choose which line to output, based on whether hasTail is true, or false. However this doesn't work for some reason, and just outputs that a human has a tail, no matter what. Please help!
    here is my code so far:
    public class HomeProject1a{
        public static void main (String [] args){
            String name = "Human";
            int numLegs = 2;
            double height = 1.8;
            boolean hasTail = false;
     
            System.out.println("The animal is a " + name);
            System.out.println("A " + name + " has " + numLegs + " legs");
            System.out.println("A " + name + " is " + height + " meters tall");
            if (hasTail = true) System.out.println("A " + name + " has a tail");
            else if (hasTail = false) System.out.println("A " + name + " doesn't have a tail");
     
        }
     
    }

    and when I run this file (shift - f6 in netbeans IDE) this is what the output is:

    The animal is a Human
    A Human has 2 legs
    A Human is 1.8 meters tall
    A Human has a tail

    if you can help thanks so much!
    skeptile


  2. #2
    Junior Member Beavis's Avatar
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: If and else if statements???

    In java you dont use boolean = true/false, you use (boolean) for true and (!boolean) for false...

    change your to last lines to this

    if (hasTail) System.out.println("A " + name + " has a tail");
    else if (!hasTail) System.out.println("A " + name + " doesn't have a tail");

  3. The Following 2 Users Say Thank You to Beavis For This Useful Post:

    Abarna (March 4th, 2013), skeptile2 (March 5th, 2013)

  4. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: If and else if statements???

    Your using '=' instead of the equality operator '=='. Like Beavis said you do not need to test a true or false value in your if-statement but use the variable itself.

  5. #4
    Junior Member Ayokings2002's Avatar
    Join Date
    Feb 2013
    Location
    Elizabeth , nj
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes u can't use = In that statement cause that would mean you are Re-assigning it with either the true or false , I could use == (equals ) , but like the other replies said , u don't need the operator

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: If and else if statements???

    Also, there is no need for the if-else-if statement. Just use if-else. for example :

    if (hasTail)
    System.out.println("A " + name + " has a tail");
    else
    System.out.println("A " + name + " doesn't have a tail");

  7. The Following User Says Thank You to StephenCoyle For This Useful Post:

    skeptile2 (March 5th, 2013)

  8. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: If and else if statements???

    OK thanks so much for the help guys. I looked at someone else's code, and they had the double equals sign == and when I tried that it worked. However I already want to have tidy code (even though I'm just starting) so what would be the tidiest way to do this? I'm thinking the !hasTail and hasTail way, but I'm not sure. Also, I did realise that I didn't need the second if, it's just that I'm trying to get the hang of multiple if else if statements.
    Thanks!

  9. #7
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: If and else if statements???

    Remember hasTail is boolean variable that can be used as an expression. Sense you want to use a control statement on that particular variable, if(!hasTail) and if(hasTail) is a viable option.

  10. The Following User Says Thank You to Zyrion For This Useful Post:

    skeptile2 (March 5th, 2013)

Similar Threads

  1. If Statements help
    By cowsquad in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2013, 06:41 AM
  2. Need help with chars/or's/if statements
    By czarcalvinsk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2012, 08:46 PM
  3. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  4. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM