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.
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");
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With