# User needs to be able to enter height like this: 5' 9"

Page 1 of 1

## 13 Replies - 383 Views - Last Post: 09 February 2018 - 02:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=409217&amp;s=41df6aee3c4f4cbe46c52d6e7f54c85b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Rina.Savage

Reputation: 0
• Posts: 7
• Joined: 09-February 18

# User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 10:05 AM

Hi, I am very new and inexperienced in JAVA and I am trying to teach myself how to write JAVA programs. What I would like to do is have a program calculate BMI. The first thing it will do is ask for the height in the form of 5'9", then the weight in pounds as just a number and then display something to the effect of, your BMI is <number>.

I can get it to function using whole numbers fine but it seems that I need to parse the input for height and I'm not really sure how to do that.

I appreciate any help you can give as this has been driving me crazy and I'm sure it's something obvious.

This is what I have so far:

```public class CalculateBMIConsole {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int height, weight;
double bmi;

Scanner keyboard = new Scanner(System.in);

System.out.print("What is your height in ft/in. You can say for" +
" example 5'9\" :" );
height = keyboard.nextInt();

System.out.print("What is your weight in lb :");

weight = keyboard.nextInt();

bmi = weight *703 / (height*height);

System.out.println("Your bmi is : " + bmi);

}

}
```

This post has been edited by ndc85430: 09 February 2018 - 10:06 AM
Reason for edit:: Added code tags. Please do this yourself in future.

Is This A Good Question/Topic? 0

## Replies To: User needs to be able to enter height like this: 5' 9"

### #2 ndc85430

• I think you'll find it's "Dr"

Reputation: 869
• Posts: 3,491
• Joined: 13-June 14

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 10:38 AM

So, you'll have to read the height as a string and extract the feet and inches from it. Thinking about a string like 5' 9", describe in words how you might find what the relevant values are. Once you have the words, we can help you figure out what the code is.

Also, please post code within "[CODE]" tags as you're asked in the box where you type your posts. I've done it for you this time.

### #3 g00se

• D.I.C Lover

Reputation: 3566
• Posts: 16,258
• Joined: 20-September 08

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 11:13 AM

Make it easier for yourself - ask for feet and inches separately

### #4 Rina.Savage

Reputation: 0
• Posts: 7
• Joined: 09-February 18

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 11:18 AM

I think the issue is understanding parsing better. It seems from how little it has been explained in the books I have that after a variable is defined and input received, there should be a line like whats below immediately after it.

```[num = Integer.parseInt(str);]
```

I've tried a few variations of this and it fails regardless.

What I have been looking for is a template that shows how parsing is used and I have probably spent like 12 hours for an example but all examples utilize whole numbers without the use of symbols " and ' which is how I want to do it.

I'm really quite amazed from all the looking around I've done that no one else has run into this issue.

### #5 g00se

• D.I.C Lover

Reputation: 3566
• Posts: 16,258
• Joined: 20-September 08

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 11:21 AM

Users are not to be trusted. You can fairly guarantee they will do it wrong, so you need to make it as idiot-proof as possible

### #6 ndc85430

• I think you'll find it's "Dr"

Reputation: 869
• Posts: 3,491
• Joined: 13-June 14

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 11:26 AM

The problem is that a value like that can't just be parsed to a number like that, since numbers don't contain characters thst aren't numbers (like '). So, you either ask for the feet and inches separately as suggested above, or you extract the numeric parts from the string.

### #7 Rina.Savage

Reputation: 0
• Posts: 7
• Joined: 09-February 18

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 11:32 AM

I completely agree with you Goose. I think taking the simple way out and making it as idiot proof as possible is the best way to design any program (among other things). However, if I really want to succeed at programming and understand it, I want to challenge myself.
The source code I provided works fine in computing the BMI when working with whole numbers, but its impractical as feet and inches are needed and as opposed to requesting feet and inches separately, I figure it's easier to just enter 5' 4" than the ladder.

If I define a String as height and work it into the parse statement:

height = Double.parsedouble(str);

then, given the (str) is this telling me that it's expecting another variable despite the fact I provided it already?

### #8 Rina.Savage

Reputation: 0
• Posts: 7
• Joined: 09-February 18

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 11:42 AM

Then the question is what am I doing wrong or missing if I want the user to input (5'9") and I have Java extract the numerical components and thus ignoring the (',")?

This is the information I have pertaining to parsing, which isn't much:

Table 2-18 Methods for converting strings to numbers
Method Use This Method To . . . Example Code

Byte.parseByte Convert a string to a byte. byte num;
num = Byte.parseByte(str);

Double.parseDouble Convert a string to a double. double num;
num = Double.parseDouble(str);

Float.parseFloat Convert a string to a float. float num;
num = Float.parseFloat(str);
Integer.parseInt Convert a string to an int. int num;
num = Integer.parseInt(str);
Long.parseLong Convert a string to a long. long num;
num = Long.parseLong(str);
Short.parseShort Convert a string to a short. short num;
num = Short.parseShort(str);

### #9 8Bit Bob

Reputation: 5
• Posts: 84
• Joined: 07-February 18

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 11:50 AM

g00se, on 09 February 2018 - 01:21 PM, said:

Users are not to be trusted. You can fairly guarantee they will do it wrong, so you need to make it as idiot-proof as possible

A programmer (by trade) once told me: "If the user always did everything right, we wouldn't have to spend so much time programming. But people always find a way to break things, so we spend most of our time making the program as idiot proof as possible."

### #10 NormR

• D.I.C Lover

Reputation: 699
• Posts: 5,358
• Joined: 25-December 13

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 12:07 PM

There is probably a pattern or regular expression that will parse the 5 and 9 from the String: 5'9"

Otherwise there are methods in the String class that can be used to find and extract the two digits from the input String.

This post has been edited by NormR: 09 February 2018 - 12:10 PM

### #11 Rina.Savage

Reputation: 0
• Posts: 7
• Joined: 09-February 18

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 02:23 PM

Is there no template or sample program that accomplishes this that I can look at? I find it extremely hard to believe I am the only one trying to do this.

### #12 modi123_1

• Suitor #2

Reputation: 14234
• Posts: 57,101
• Joined: 12-June 08

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 02:26 PM

Certainly there is.. but just doing it for you sort of ruins the whole educational product. />

Example - you could use strtok to make a collection of strings after breaking a string into parts.. say around the 'feet' mark.
http://www.cplusplus...cstring/strtok/

Why did I read this as c++?! Ignore me.. ugh.

This post has been edited by modi123_1: 09 February 2018 - 02:46 PM

### #13 g00se

• D.I.C Lover

Reputation: 3566
• Posts: 16,258
• Joined: 20-September 08

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 02:37 PM

Try this

```String[] feetAndInches = input.replaceAll("[^ 0-9]", "").split(" ");
```

Of course that won't work properly if the user doesn't include space as well.
The much bigger problem is that if your user is entering symbols like the feet (single quote) and inches (double quote) ones at the command line, s/he is also going to have to be a master of shell escapement - good luck with that

This post has been edited by g00se: 09 February 2018 - 02:53 PM

### #14 NormR

• D.I.C Lover

Reputation: 699
• Posts: 5,358
• Joined: 25-December 13

## Re: User needs to be able to enter height like this: 5' 9"

Posted 09 February 2018 - 02:59 PM

Quote