Page 1
Page 1
Started By
Message
locked post

Help me with my nested if statements in java (self-learning Novice)

Posted on 12/14/13 at 8:13 am
Posted by Pectus
Internet
Member since Apr 2010
67302 posts
Posted on 12/14/13 at 8:13 am
I am having trouble with the following code that is supposed to ask questions about gender and age, and if over 20, ask if the user is married. If they a woman, 20+, and married then I title them "Mrs.", if woman, not married, but 20+, "Ms.", if man of 20+ years, and married or not, "Mr.", and if any user is under 20: just their name is put back without any title.

I feel like I've tried many ways to get this to work. I am trying to use nested ifs and AND in this code. The operations should make sense with the way I've structured them yet, if someone is 20+ and married, they always come out as a Mr.

Any help would be appreciated. TIA

The code.
quote:


import java.util.Scanner;

public class GenderGame
{
public static void main( String[] args )
{

Scanner keyboard = new Scanner(System.in);

String gender, first_name, last_name, married;
int age;

System.out.println( "What is your gender? (M or F):" );
gender = keyboard.next();
System.out.println( "First name: " );
first_name = keyboard.next();
System.out.println( "Last name: " );
last_name = keyboard.next();
System.out.println( "Age: ");
age = keyboard.nextInt();

String male, female, yes, no;

male = "M";
female = "F";
yes = "Y";
no = "N";



if( age >= 20 ) {
System.out.println( "Are you married, " + first_name + "? (Y or N) " );
married = keyboard.next();
if( gender == female && married == yes ) {
System.out.println( "Then you are called Mrs. " + first_name + " " + last_name + "." );
}
else if( gender == female && married == no ) {
System.out.println( "Then you are called Ms. " + first_name + " " + last_name + "." );
}
else {
System.out.println( "Then you are called Mr. " + first_name + " " + last_name + "." );
}
}

else {
System.out.println( "Then you are called " + first_name + " " + last_name + "." );
}

}
}




Posted by tom
Baton Rouge
Member since Jun 2007
8158 posts
Posted on 12/14/13 at 8:42 am to
Shouldn't your if statements be looking for married = Y or N instead of yes or no?
Posted by Pectus
Internet
Member since Apr 2010
67302 posts
Posted on 12/14/13 at 9:08 am to
I have an input of "Y" = to the variable yes to compare it to other variables later.

But if that's the issue, then it explains why I only get Mr. (due to the else).
This post was edited on 12/14/13 at 9:10 am
Posted by Mr Gardoki
AL
Member since Apr 2010
27652 posts
Posted on 12/14/13 at 9:09 am to
If you're going to be doing a lot more of this then I recommend this site. Stackoverflow
A lot of programmers go there when they get stuck.
Posted by Eternalmajin
Member since Jun 2008
13071 posts
Posted on 12/14/13 at 9:55 am to
What you can do in situations like this is, right before the:
if(gender == female && married = yes)

block, print out the values of gender, female, married, and yes. This will let you see, from the program's perspective, what those values are and make sure they match up.

If they match up, it may be an issue of comparing strings. I'm not sure if java has different functions for strings like this or not, but I know C had strcmp() for comparing them.

Just by looking at it, I have a feeling the program is seeing a difference between declaring a variable as a string and reading one in from keyboard.next()
This post was edited on 12/14/13 at 10:00 am
first pageprev pagePage 1 of 1Next pagelast page
refresh

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram