5

I have decided to try and learn a little in java tonight and i have just been trying some stuff with things i have learned.

My question is in an if statement how to i make two stings to be true. Here is what i have so far.

if ("male".equals(gender)) && ("brendan".equals(name))

The problem i am pretty sure is the && but i am not sure.

EDIT EDIT EDIT.

This fixed part of the problem.
if("male".equals(gender) && "Brendan".equals(name))
and so did this

if (("male".equals(gender)) && ("brendan".equals(name)))

Now i will post the whole thing i am having another issue now and you would probably need to see the whole thing.

import java.util.Scanner;

public class my_input_and_if_statements 
{
    public static void main (String args[])
{
    Scanner jonks = new Scanner(System.in);
    String name, gender, answer;
    System.out.println("What is your name?");
    name = jonks.next();
    System.out.print("Hello ");
    System.out.print( name);
    System.out.println(" are you male or female?");
    gender = jonks.next();

        if (("male".equals(gender)) && ("brendan".equals(name)))            
            {
            System.out.println("blah blah");
            }

        else
            {
            System.out.println(" Welcome to one of my first java applications. I hope you like it");
            }

        if (("female".equals(gender)) && ("nicole".equals(name))) 
            {
            System.out.println("blah blah 2");
            }
        else
            {
            System.out.println(" Welcome to one of my first java applications. I hope you like it");
            }

            }
}

sorry if this seems kind of pointless just trying to tie some stuff in together before i start trying to learn some more.

now when i go through it gives me two lines when it finishes or it gives me both.

user1820578
  • 63
  • 1
  • 1
  • 4

4 Answers4

1
if (("male".equals(gender)) && ("brendan".equals(name)))
   ^                                                   ^

if(condition) needs the enclosing brackets.

For your multiple comparisons, you could have a male or a female, not both, so you could use a nested if..else construct instead,

Otherwise, you will perform the male/female evaluation for every name, unnecessarily. The following seems more natural to me.


if ("male".equals(gender)){
    if("brendan".equals(name)){
        System.out.println("right");   
    }else{
        System.out.println("wrong wrong");  
    }
    //add more cases here
}else{
    //definitely female

}
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • Never thought about doing it this way but definatly makes sense when you think about it. but it opens up another question. – user1820578 Nov 13 '12 at 11:28
  • like you have above. how do you do the second else? so it has
        if ("male".equals(gender)){
          if("brendan".equals(name))
             {
             System.out
             }
        else
             {
             System.out
             }
    
        //this part?
        else
    
    – user1820578 Nov 13 '12 at 11:33
  • it is not formating properly. Give me a sec. – user1820578 Nov 13 '12 at 11:35
  • @user1820578 In that case, your `&&` based solution might be better. But if you want to add more conditions later, it will be harder. I'd implement it in the else like `System.out.println(" wrong wrong " + gender);` in order to differentiate between the 2 failed cases. – Anirudh Ramanathan Nov 13 '12 at 11:37
  • @user1820578 I have updated my answer. In your code, you are testing for female, even after ascertaining if male, in the above loop, which is IMO unnecessary. – Anirudh Ramanathan Nov 13 '12 at 11:41
  • I cant post this to many characters. Its a tomorrow job now any way. Thanks for your help. – user1820578 Nov 13 '12 at 12:02
1

I think the issues is your parentheses.

An if statement has the form if(condition), where condition is some boolean expression. In this case you're wanting the condition to be "male".equals(gender) && "Brendan".equals(name), so your complete statement should be something like the following:

if("male".equals(gender) && "Brendan".equals(name)) {
    //Foo Blah blah
}
else {
    //Barr fuzz fuzz
}

For the second part of your question, you have two if statements, both of which print on both the if and the else branches, so you're going to hit one or other of the println statements in the first if and one or other of the println statements of the second if. I think what you probably wanted to do was something like the following:

if(gender.equals("male") && name.equals("Brendan")) {
    println("Brendan's Message");
}
else if(gender.equals("female") && name.equals("Nicole")) {
    println("Nicole's Message");
}
else {
    println("Anybody's Message");
}
Edd
  • 3,724
  • 3
  • 26
  • 33
1

One missing (, and superfluous parentheses.

if ("male".equals(gender) && "brendan".equals(name))
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I don't think the braces are superfluous, although it's a bit of a contentious issue: http://programmers.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no – Edd Nov 13 '12 at 11:05
  • @Edd sorry, I meant parentheses. – Joop Eggen Nov 13 '12 at 12:40
0

I think he wants to learn about enums. like

 public enum Gender
 {
      MALE = "Male";
      FEMALE = "Female";
 }

or this Best way to create enum of strings?

Community
  • 1
  • 1
Shark
  • 6,513
  • 3
  • 28
  • 50