0

Ok, so I am trying to figure out whay no matter what I enter for gender it tells me the gender is invalid. My goal is for the program to recognize m or M or F or f as gender, which I thought I accomplished by .toUpperCase... Anyone know what I am missing?

public class ChildHeight {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in); //iniate keyboard as scanner object


        String cGender;
        int hMotherFeet;
        int hMotherInches;
        int hFatherFeet;
        int hFatherInches;
        int hChild;
        int hMother;
        int hFather;

      //Prompts user for imput
        System.out.print("Please enter gender for a child (ex. m,F): ");
        cGender = keyboard.next();
        System.out.print("Please enter the number of feet in father's height: ");
        hFatherFeet = keyboard.nextInt();
        System.out.print("Please enter the number of inches in father's height: ");
        hFatherInches = keyboard.nextInt();
        System.out.print("Please enter the number of feet in mother's height: ");
        hMotherFeet = keyboard.nextInt();
        System.out.print("Please enter the number of inches in mother's height: ");
        hMotherInches = keyboard.nextInt();

      //adds inches to the converted inches from feet
        hMother = hMotherInches + ConvertToInches(hMotherFeet);
        hFather = hFatherInches + ConvertToInches(hFatherFeet);

      //takes parameters and based on gender points to formula to get childs height
        hChild = GetChildHeight(hMother, hFather, cGender);

      //Prints childs calculated height
        System.out.printf("The estimated child height is %d'%d\".", (hChild / 12), (hChild % 12));
    }

    //method to convert feet to inches
    public static int ConvertToInches(int feet)
    {
        return feet * 12;
    }

    public static int GetChildHeight(int mother, int father, String gender)
    {
        int child = 0;

        if (gender.toUpperCase() == "M")
        {
                child = ((mother * 13/12) + father)/2;
                }
            else if (gender.toUpperCase()== "F")
            {
                child = ((father * 13/12) + mother)/2;
            }
                else
            {
            System.out.println("Invalid gender. Gender must be M or F.");
                System.exit(0);

        }

        return  child;
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Jun 21 '15 at 21:33
  • So not, `if (gender.toUpperCase() == "M")`, but rather, `if ("M".equalsIgnoreCase(gender))` – Hovercraft Full Of Eels Jun 21 '15 at 21:34
  • I see the error I made, and feel exceptionally dumb for missing that completely... the Java coding will be the death of me! :) thanks for explaining that! – Goldie Anne Johnson Jun 21 '15 at 22:44
  • Not to worry. The important thing is that you won't make it again, and that's all that matters. – Hovercraft Full Of Eels Jun 21 '15 at 22:52

0 Answers0