13 Replies - 354 Views - Last Post: 09 February 2018 - 02:59 PM Rate Topic: -----

#1 Rina.Savage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 798
  • View blog
  • Posts: 3,258
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Rina.Savage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is offline

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

Reputation: 798
  • View blog
  • Posts: 3,258
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 Rina.Savage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 Rina.Savage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
Was This Post Helpful? 0
  • +
  • -

#9 8Bit Bob  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 66
  • Joined: 07-February 18

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

Posted 09 February 2018 - 11:50 AM

View Postg00se, 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."
Was This Post Helpful? 0
  • +
  • -

#10 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 Rina.Savage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

That's really not helpful.

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.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 14032
  • View blog
  • Posts: 56,119
  • 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

Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • 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

Was This Post Helpful? 0
  • +
  • -

#14 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • 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

That's really not helpful.

It might have pushed g00se to write the reg exp code for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1