2

I'm working on a workshop for my JAVA. There's one part I'm having difficulty understanding: Apply the following formulas based on gender (must use an if statement(s)): Hmale_child = ((Hmother * 13/12) + Hfather)/2 OR Hfemale_child = ((Hfather * 12/13) + Hmother)/2

How do I use an if statement with these formulas? I use the Hmale_child if the user inputs that their child's gender is male. But all the if statements I've seen have to do with numbers. Can anyone point me in the right direction?

Something like: if (gender == male) ??


import java.util.Scanner;

public class WKSP6Trial
{  
public static void main(String[] args)
{
  Scanner scannerObject = new Scanner(System.in);

  Scanner inp = new Scanner(System.in);
  String Letter; 
  String input = "";
  String exit = "exit";
  boolean isStringLetter = true;
  System.out.println("Welcome! If at any time you wish to exit the program, type the word exit, and press enter.");

  while(true)
  {
      System.out.println("\nPlease enter letters m or f only as you enter your child's gender: ");
      input = inp.nextLine();

      if(input.equalsIgnoreCase(exit)){break;}
      isStringLetter = input.matches("[m/f/M/F]+");
      if(isStringLetter == false)
      {
         System.out.println("\nYou entered a non letter " + input);
         System.out.println("Remove all non letters aside from m or f from your input and try again !");
         break;
      }

  System.out.println("You entered the gender of your child.");

  System.out.println("Next enter the height of the child's father in feet followed by ");
  System.out.println("the father's height in inches: ");

  int n1, n2;
  n1 = scannerObject.nextInt();
  n2 = scannerObject.nextInt();
  System.out.println("You entered " + n1 + " followed by " + n2);

  System.out.println("Finally, enter the height of the child's mother followed by ");
  System.out.println("the mother's height in inches: ");

  int d1, d2;
  d1 = scannerObject.nextInt();
  d2 = scannerObject.nextInt();
  System.out.println("You entered " + d1 + " followed by " + d2);

  Scanner in = new Scanner(System.in);

     System.out.print("Convert from: ");
      String fromUnit = in.nextLine();
      System.out.print("Convert to: ");
      String toUnit = in.nextLine();


      //below is what I'm uncertain of
      if(gender == m)
      Hmale_child = ((Hmother * 13/12) + Hfather)/2;
  }
}
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
J. Nav
  • 55
  • 2
  • 9
  • To compare strings, you don't usually use `==`. Instead, you probably want to write `if (gender.equals("M"))` or `if (gender.equalsIgnoreCase("M"))` or something similar. For more information on why, look at the answers to [this question](http://stackoverflow.com/q/513832/) – Dawood ibn Kareem Oct 07 '16 at 20:17
  • When I do this, I immediately get the error, "cannot find symbol". That means I have to declare and initialize the variable, right? So I set gender.equals, Hmother, and Hfather as Strings? – J. Nav Oct 07 '16 at 20:27
  • Oops, sorry, you called it `input`, not `gender`. So, `if (input.equals("M"))`. – Dawood ibn Kareem Oct 07 '16 at 20:28
  • That seems to work, I just have to declare Hmother and Hfather. Would I declare them as Strings? – J. Nav Oct 07 '16 at 20:32
  • No. Multiplying and dividing Strings doesn't really make sense. Declare them as whichever datatype best matches what you're trying to model with them. – Dawood ibn Kareem Oct 07 '16 at 20:33
  • Thank you for all your help and advice! That really helps. – J. Nav Oct 07 '16 at 20:45
  • OK. Just make sure you read the answers to that other question that I linked to. I think you'll learn a lot from them. – Dawood ibn Kareem Oct 07 '16 at 20:46

3 Answers3

1

You can use the equals or equalsIgnoreCase methods to check your string inputs:

if (gender.equalsIgnoreCase("m")) {
    child = ((Hmother * 13.0/12) + Hfather)/2.0;
else { // alternatively - check if the gender is female, just to be sure
     child = ((Hfather * 12.0/13) + Hmother)/2.0;
}

EDIT:
If you absolutely cannot use an else block, the same logic can be expressed as two if statements, as a gender can't be both "m" and "f":

if (gender.equalsIgnoreCase("m")) {
    child = ((Hmother * 13.0/12) + Hfather)/2.0;
}

if (gender.equalsIgnoreCase("f")) {
     child = ((Hfather * 12.0/13) + Hmother)/2.0;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

As in Java, String is an object. So the appropriate way to check or compare your input string is to use equals() method instead of ==.

For example : String s = "m"; to check whether String s equals m, you can use s.equals("m").

For int, double and other primitive datatypes (except boolean) you can use ==.

As boolean holds only two values i,e true or false. You can just use them directly in the if conditions.

For example

boolean b = true;
if (b){
 // true
}else{
//false
}
Ravikumar
  • 891
  • 12
  • 22
  • Well, I can't use "else". He specifically asked for an "if statement". Thanks for the quick response, though – J. Nav Oct 07 '16 at 20:31
  • @J.Nav Whenever you use an `if` statement its always better to add an `else` as if/else defines your program in a clear and cleaner way, although you can just use an if without an else. – Ravikumar Oct 07 '16 at 20:36
0

You can compare Strings in if statements too.

Bear in mind that Strings are objects, so == won't work as you might expect. When comparing objects, == checks if the two object references point to the same object, so two different objects with identical data will not be considered the same using ==. If you want to compare the contents of the object, you should use the equals() method instead.

Most standard Java objects should support a reasonable definition of equality through their equals() method. In the case of Strings, the content of the String will be compared in a case-sensitive manner (use equalsIgnoreCase() if you don't want the comparison to be case sensitive.)

If you make your own classes, you will need to implement a reasonable definition of equals() yourself.

(The == and .equals() rule is slightly more complicated in the case of Strings due to Java's String table, so == often will work in that case - but you still shouldn't rely on it.)

So, to run a different calculation based on the entered gender, you would do something like this:

float height;
if ( gender.equalsIgnoreCase("m") ) {
    height = (heightMother * 13f / 12f) + (heightFather / 2f);
else {
    height = (heightFather * 12f / 13f) + (heightMother / 2f);
}

Note the names of my variables - Java convention is for variable names to start be in camelCase - variable names should not start with a capital letter.

Also, I've used "f" at the end of my numbers to indicate that they are floating-point numbers - you will lose precision if you perform all the mathematics using integers.

Nameless Voice
  • 461
  • 2
  • 9