Hi! I recently started teaching Java to myself yesterday via TheNewBoston on youtube. I began working on a few programs from the programmingbydoing website but I'm stuck on this problem:http://programmingbydoing.com/a/gender-game.html

It's a problem focusing on nested if statements.
Here's my code!:

import java.util.Scanner ;
public class MainClass {
    public static void main(String args []) {

    String gender, last, first, status;
    int age; 
    Scanner response = new Scanner(System.in);

    System.out.print("What is your gender (M or F): ");
    gender = response.nextLine();
    System.out.print("First name: ");
    first = response.nextLine();
    System.out.print("Last name: ");
    last = response.nextLine();
    System.out.print("Age: ");
    age = response.nextInt();


    if (gender.equals('M')) {
        if (age >= 20) 
        {System.out.printf("Then I shall call you Mr. %s.", last);}
        else {System.out.printf("Then I shall call you %s %s.", first, last);}
    }
    else {
        if (age >= 20){
            System.out.printf("Are you married, %s (y or n)?", first);
            status = response.nextLine();
            if (status.equals('y')){
            System.out.printf("Then I shall call you Mrs. %s.", last);
            }
            else {System.out.printf("Then I shall call you Ms. %s.", last);}
        }
        else {System.out.printf("Then I shall call you %s %s", first, last);
        }



    }
    }
}

Right now, when I run the code, I get an output such as this whenever I put the age over 20

What is your gender (M or F): M
First name: John
Last name: Smith
Age: 30
Are you married, John (y or n)?Then I shall call you Ms. Smith.

The program does not bother to prompt me for marriage status and directly assumes I'm an unmarried female. Can someone help me spot the error within my nested if statement? I've rewritten it 2 times already and am still unable to spot the problem.

You've run into a very common problem with Scanner. After nextInt() the following new line character is left unread, so when you do the next nextLine() you get a zero-length string ie ""
It's a messy bit of design in Scanner, not your fault really.
When yu have a nextInt() follwed by a nextLine(), you need to add an extra dummy nextLine() just to get rid of the new line character

it might be better still to start at the basics. if you'd known all data types you would realize that a char (or the Character wrapper class) is a lot more appropriate for your gender then a String.

You've run into a very common problem with Scanner.

I often wonder why it seems to be such a common problem. The name is misleading, but only because nextLine parallels nextInt, nextFloat, and the other methods so that you might think they have parallel meanings, when actually nextLine means "skip to the next line" instead of "get the next line." If it weren't for those other methods being named that way, nextLine would be a perfect name for the method.

What puzzles me most is that the Javadoc clearly explains with nextLine does, that it is returning text from the current line, so confusion shouldn't be common. Surely the world isn't filled with people who habitually guess what methods are supposed to do based on name alone.

In this case it seems likely that only one thing will ever be read from each line, so it might be simpler to use useDelimiter("\n") to set the scanner to use the end of each line as its delimiter. That way you can use next instead of nextLine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.