1

My program is not working as it should.

Purpose of the code: ask user name, gender, age - if age over 18, ask user if married. if user says yes, output Mr or Mrs in front of already given name.

I'm practicing nested if statements and getter/setter methods to return private variables.

I have tried: nesting the if statement that tests the gender and age status, then i tried using a switch statement instead of an if statement to test the marital status, then i tried nesting an if statement within the switch to test the gender and age. I have tried giving each of the if, else if clauses their own scanner input thinking that would just get it to function, but it did not.

I originally had the marital status as a getter/setter method, but took it out to simplify the problem.

I have looked for an answer, but it seems like the code is right, it just will not take the input!

The code I have is as follows:

package agemessage;

import java.util.Scanner;

public class AgeMessage {

    public static void main(String[] args) {
        info infoObject = new info(); //setter getter class to house input from user
        Scanner in = new Scanner(System.in);

        System.out.println("what is your name again?");
        String input = in.nextLine();
        infoObject.getName(input);

        System.out.println("thanks, " + infoObject.setName() + " is that a boys name or a girls name?");
        String gender = in.nextLine();
        infoObject.getGender(gender);

        System.out.println("How old are you " + infoObject.setName() + "?");
        int ageInput = in.nextInt();
        //infoObject.getAge(ageInput);

        if (ageInput < 18) {
            System.out.print("I shall just call you " + infoObject.setName() + ".");
            System.exit(0);

        } else if (ageInput >= 18) {
            System.out.println("Are you married yet?");
        }

        //PROGRAM STOPS HERE -- DOES NOT EXECUTE INPUT 
        String status = in.nextLine();

        if (status.equalsIgnoreCase("Y") && infoObject.setGender().equalsIgnoreCase("Girl")) {
            System.out.println("I will have to call you Mrs. " + infoObject.setName() + ".");

        } else if (status.equalsIgnoreCase("Y") && infoObject.setGender().equalsIgnoreCase("Boy")) {
            System.out.println("I will have to call you Mr. " + infoObject.setName() + ".");
            System.exit(0);

        } else if (status.equalsIgnoreCase("N")) {
            System.out.println("I will just call you " + infoObject.setName() + ".");
        }

    }// main end

}//class end

OUTPUT:

run:
what is your name again?
Carrie Ann Moss
thanks, Carrie Ann Moss is that a boy's name or a girls name?
girl
How old are you Carrie Ann Moss?
40
Are you married yet?
BUILD SUCCESSFUL (total time: 16 seconds)
RealSkeptic
  • 32,074
  • 7
  • 48
  • 75
LunaC
  • 11
  • 2
  • 3
    Seems like a very similar problem to this: [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods). It is also a bit confusing to use `setName`/`setGender` as the getters and `getName`/`getGender` as the setters. – Obicere Aug 11 '15 at 17:38
  • 1
    Please follow coding standards. Whatever your POJO class is, generally getters doesn't have some arguments to it and setter generally have arguments in it but your code looks just opposite. Please modify the code and post it again – Kulbhushan Singh Aug 11 '15 at 17:41
  • The immediate cause of program aborting is -no doubt- `System.exit`. Presence of `System.exit` calls in a program do not help at all. I suggest you drop them all. At much, leave just one, at the end of the program. – Little Santi Aug 11 '15 at 17:47
  • The program never stops: it continues executing until the end of the main function as designed. System.exit() is not the issue in this case. –  Aug 11 '15 at 18:25

3 Answers3

0

Right now you don't have a proper setter, add one and see if it works.

Ruchir Baronia
  • 6,857
  • 4
  • 45
  • 75
0

Please try putting in.nextLine(); after int ageInput = in.nextInt();

In this way the scanner will not ignore the next line after Integer.

A possible duplicate would be Using scanner.nextLine()

Community
  • 1
  • 1
Kesavacharan
  • 218
  • 3
  • 13
0

There are a few things that need to be addressed before we solve your problem.

Code conventions. These help others read your code. Not required, but it does help.

Getters and setters: A getter gets the object. A setter sets the object. You're using them in reverse. Example below.

class Person{
    int age;

    public void setAge(int a){
        age = a;
    }

    public int getAge(){
        return age;
    }
}

So in a program, let's say we have a Person object, called annie, who is 22 years old.

Person annie = new Person();
annie.setAge(22);

Later in the program, we want to say how old she is...

System.out.println("Annie is " + annie.getAge() +" years old.");

The getter gets the value of the age variable on the Person object called annie.


Now for the actual problem in your program, when nextInt is called, it does not parse the newline character. So what is happening is this:

int ageInput = in.nextInt();

Here, when you enter "40" and then press Enter, you're giving the program this:

"40\n"

nextInt only reads to the end of the integer, so it only reads the 40, not the newline character.

if (ageInput < 18) {
    System.out.print("I shall just call you " + infoObject.setName() + ".");
    System.exit(0);

} else if (ageInput >= 18) {
    System.out.println("Are you married yet?");
}

This works correctly, printing out the "Are you married yet?" string. However, it doesn't stop executing.

String status = in.nextLine();

Here's the problem. Your Scanner object, it hasn't passed beyond the previous newline character. So it immediately accepts the rest of the line, which is simply this:

"\n"

So the String status is then set to "\n".

if (status.equalsIgnoreCase("Y") && infoObject.setGender().equalsIgnoreCase("Girl")) {
    System.out.println("I will have to call you Mrs. " + infoObject.setName() + ".");

} else if (status.equalsIgnoreCase("Y") && infoObject.setGender().equalsIgnoreCase("Boy")) {
    System.out.println("I will have to call you Mr. " + infoObject.setName() + ".");
    System.exit(0);

} else if (status.equalsIgnoreCase("N")) {
    System.out.println("I will just call you " + infoObject.setName() + ".");
}

Since status is set to "\n", nothing else is printed, and the end of the main function is reached, terminating the program.

To fix this, put a in.nextLine() directly after in.nextInt(). This skips the rest of the line, allowing your program to continue into the previously skipped sections.

Community
  • 1
  • 1
  • 1
    Thank you, I seem to remember (now that you have thoroughly explained it) that nextInt needed special care and consideration. I never would have figured this out without you. My gratitude!! I thought it was weird that set was getting the variable - it was explained to me backwards :-/ thank you thank you thank you!! – LunaC Aug 18 '15 at 05:16
  • You're very welcome. Since I seemed to have answered your question, would you be so kind as to accept my answer? :) –  Aug 18 '15 at 14:20