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), whereconditionis 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:For the second part of your question, you have two
ifstatements, both of which print on both theifand theelsebranches, so you’re going to hit one or other of theprintlnstatements in the firstifand one or other of theprintlnstatements of the secondif. I think what you probably wanted to do was something like the following: