0
public class TestingIf-Else //My class for determining child height
{
  public static void main(String[] args)
   {
  //Setting my variables.
  DecimalFormat neat = new DecimalFormat("#.00"); // Setting neat to two decimals
  Scanner keyboard = new Scanner(System.in); // Initalizing keyboard to scanner input
  int feetD, inchesD;
  int feetM, inchesM;
  double Hmale_child, Hfemale_child;
  String gender;


     //Prompt for gender
  System.out.println("Enter gender here. Either M for male, or F for female");
  gender = keyboard.next();


     if (gender.equalsIgnoreCase("m"))
   {gender = "Male";}


     else if (gender.equalsIgnoreCase("f"))
   {gender = "Female";}

     else
   {gender = "Incorrect input, try again.";
                         System.exit(0);}

  System.out.println("This is the input for gender: " + gender);
     // Inputs for fathers height, feet and inches.
  System.out.println("Fathers height in feet: ");
  feetD = keyboard.nextInt();
  System.out.println("Fathers height in inches: ");
  inchesD = keyboard.nextInt();

     // Inputs for mothers height, feet and inches.
  System.out.println("Mothers height in feet: ");
  feetM = keyboard.nextInt();
  System.out.println("Mothers height in inches: ");
  inchesM = keyboard.nextInt();

     //Converting both heights to inches.
  double totInchesD = ((feetD * 12) + inchesD);
  double totInchesM = ((feetM * 12) + inchesM);

     //Determining estimated child height.
  if (gender.equals("Male"))
  {
     Hmale_child=((totInchesM * (13/12) + totInchesD)/2);
     System.out.println(neat.format(Hmale_child));
  }
  else 
  {
     Hfemale_child=((totInchesD * (13/12) + totInchesM)/2);
     System.out.println(neat.format(Hfemale_child));
     System.out.println("Check");
    }
  }
}

I've sat here for two days trying to logically go through, i need another set of eyes please! I don't understand it, no matter the case, all values for both Hmale, and Hfemale child all come out to the same.

Dillon
  • 15
  • 6
  • Always compare strings using equals.... else if (gender!=("Male")) – Juned Ahsan Oct 08 '15 at 01:41
  • @JunedAhsan What do you mean? Should i only use if (gender==("Male") and else if (gender!=("Male")? – Dillon Oct 08 '15 at 01:44
  • Two problems, you should compare string using == or !=. String comparison should always be done using equals() method. Secondly, you don't need an else if to check the reverse of first condition, just use only else. – Juned Ahsan Oct 08 '15 at 01:48
  • Use if (gender.equals("Male")) . Checkout http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?lq=1 – sean Oct 08 '15 at 01:51
  • @sean Okay! I just read that post! Thanks man, I appreciate it! Another notch on my belt. But still, the problem persists. Height of both male and female children return the same value no. Provided that height of father and mother remains constant in both cases. – Dillon Oct 08 '15 at 01:54
  • @Dillon Change your lines to this Hmale_child=((totInchesM * (13.0/12.0) + totInchesD)/2.0); – sean Oct 08 '15 at 02:10
  • @sean But, isnt that irrelevant because being stored in a double pretty much means it will contain decimals ? Am I thinking about double incorrectly? – Dillon Oct 08 '15 at 02:14
  • This is an arithmetic problem . int w = 7 ; double x = w/2.0*10;// x is 35 ; double x = w/2*10; // x is 30 . Try it . – sean Oct 08 '15 at 02:16

1 Answers1

0

You could store the gender as a boolean. And you might use a loop to get the input. Something like,

// Prompt for gender
String gender = "Male";
boolean isMale = true;
while(true) {
    System.out.println("Enter gender here. Either M for male, or F " 
            + "for female");
    String val = keyboard.next().trim();
    if (val.equalsIgnoreCase("m")) {
        // gender = "Male";
        // isMale = true;
        break;
    } else if (val.equalsIgnoreCase("f")) {
        gender = "Female";
        isMale = false;
        break;
    } else {
        System.out.println("Incorrect input, try again.";
    }
}
// ...
if (isMale) {
    // ...
} else {
    // ...
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • You sir. Have officially blown my mind. Yet, I haven't studied much of boolean nor have I gotten far enough into java to understand several of these methods or calls. such as break; <-- What is the significance of this. By the way, this is brilliance. – Dillon Oct 08 '15 at 02:06