-1

So in my homework I need to write a program that asks for the users height and weight and convert it from feet, inches to meters, cm. As well as convert the weight from pounds to kilograms. The program I've got would not be able to take a measurement such as 5'7 and convert it. Here is the acutual homework question:

  1. The program asks the name of the user. The program will prompt "What is your name?" the user types his/her name.

    1. The program should then ask for height and weight of the user.

    2. It should then print on separate lines, "Name of the User" your height in metric system is (prints out the height in Meters and cm. For example, your height in metric system is 1 meter 45 cm.

    3. It should then print the weight in KG and grams (same as above your weight in metric system is ----)

Here is what I have come up with thus far:

package extracredit;

/**
 *
 * @author Kent
 */
import java.util.Scanner;

public class Extracredit {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("What is your name? ");
        String name = in.next();
        System.out.println("What is your height? ");
        Double Height = in.nextDouble();
        System.out.println("What is your weight? ");
        Double Weight = in.nextDouble();
        System.out.println(name);
        System.out.print("Your height in metric system: ");
        System.out.println(Height * .3048);
        System.out.println("Your weight in kg: ");
        System.out.println(Weight*0.453592);
    }

}

4 Answers4

1

The simple solution is this.

You ask "what is your height in feet", let that be int feet.

Then "and how many inches", let that be int inches.

Then assign int totalInches = 12*feet + inches.


Other solutions would include splitting the string on the ' character and parsing the intvalues out.

Or using a regular expression, but I'm guessing that would be a more advanced topic than you are seeking at the moment.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • +1 The important lesson here is that if a school assignment is underspecified, feel free to exploit those holes. By doing so you'll learn how to write exact specifications later in your career. :) – biziclop Feb 09 '16 at 23:49
  • @biziclop - Like I said, the other answers would be more advanced than what is shown in the question. Personally, I knew nothing about regex when I learned about `Scanner` – OneCricketeer Feb 10 '16 at 00:02
0

You could try this approach, it will ask for the height in the feet'inches" format and then convert it to inches/centimetres.

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter your height (feet'inches\")");
    String heightInput=in.next();
    int heightInInches;
    heightInInches=Integer.parseInt(heightInput.split("'")[0])*12;
    //Calculate feet
    heightInInches+=Integer.parseInt(heightInput.split("'")[1].replaceAll("[^\\d.]",""));
    //Celculate remaining inches, regex is used to remove the "

    System.out.println("Height in inches: "+heightInInches);
    System.out.println("Height in centimetres: "+heightInInches*2.54);
    //Multiply by 2.54 to convert to centimetres
Syd Lambert
  • 1,415
  • 14
  • 16
0

If you are accepting formatted input then you should likely use an explicit regular expression with groups to represent the values. That way you get all the error checking for free as part of the parsing.

For example:

Pattern heightPattern = Pattern.compile("(\\d+)'(\\d+)\"");
Matcher heightMatcher = heightPattern.match(in.next());
while (!heightMatcher.matches()) {
    System.out.println("Please re-enter your height in the format n'n\"");
    heightMatcher = heightPattern.match(in.next());
}
int feet = Integer.valueOf(heightMatcher.group(1));
int inches = Integer.valueOf(heightMatcher.group(2));
sprinter
  • 27,148
  • 6
  • 47
  • 78
-2

You could just get the input as a String and split it like

String height = in.next();
String[] splitHeight = height.split("'");
int feet = Integer.valueOf(splitHeight[0]);
int inches = Integer.valueOf(splitHeight[1]);

Here you'd have to consider that splitHeight[1] might be null (if the input String does not contain a ' ). Afterwards you can just do your calculations in inches with feet * 12 + inches.

Valentin Kuhn
  • 753
  • 9
  • 25