• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

While loops

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am making a program that estimates the height of a child based off gender, height of mother, height of father, and given equations. The program should allow the user to enter a new set of values and output predicted height until user decides to exit. Here is my code. When I run the program, first of all the calculations are coming out wrong and secondly, after the loop has ran, it skips the second gender/stop input line and goes straight to asking for height of father again.
I have attached a copy of my output.



Picture 1
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) I added code tags to your post - it makes it MUCH easier to read. Please start using them in the future. Just highlight your java and click the 'code' button as if you wanted to make it all bold or italic.

2) Please don't post an image of your output. You can cut'n'paste from a command window or terminal session and paste the results in as normal text. again, it makes it easier to read.

3) How are the calculations coming out wrong? What is coming out, and what do you expect to come out?
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Calculations:

In Java, if you divide one integer by another, you get another integer. Integers are always rounded down. So 12 / 13 = 0, and 13 / 12 = 1. You can resolve this by making one or both of the numbers into a float or double, with an "f" or "d" postfix, or decimal point, e.g. 12.0 / 13 is 0.923...., 13 / 12d is 1.083....

2) Exceptions:

This is due to your use of `nextInt` when your input string was not an int. You need to validate that the input is actually 2 numbers separated by a space and decide what to do if it's not.
 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is the input not an integer though? I am asking for two whole numbers, feet first then inches. When I replace '12' with 12.0, I get an error: "loss of precision".

The output is wrong because if I input '10' then '0' for height of both parents it ouputs that the height of the male child will be 10 feet and 0 inches. The correct answer should be 10 feet and 5 inches roughly.

And I still don't understand why it is not allowing user input after asking: "Enter another gender to keep going or 'stop' to quit." It goes straight to the next question.

Output looks like this:

Enter another gender to keep going or 'stop' to quit.
Enter father's height, feet first then inches.
Seperate feet and inches by a space.
(Then allows user input)
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't stop because after you used `nextInt` it's still working on the same line of input. `nextLine` just takes the rest of that line of input up to the line end. Try adding another `nextLine`.

If you want your values rounded down to ints you can do a cast, i.e. `e.g. int i = (int) 1.1;`(add 0.5 beforehand if you want to round to the nearest int), but in genereal it's better to keep the accurate value and simply display it to the required precision using `printf` or `String.format`. In other words you need to store the values as doubles.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Collin Sampson wrote:How is the input not an integer though?



Since you didn't show the input or the error, it's impossible to say for sure. All we can say for sure is that it didn't meet the criteria of consisting of nothing but digits, optionally with a leading minus sign, and representing a number within int's range.

I am asking for two whole numbers, feet first then inches. When I replace '12' with 12.0, I get an error: "loss of precision".



The "loss of precision" error is telling you just what it sounds like: If you try to stick a double value into an int variable, you will lose some precision. That is, it will not be as precise. So, for instance, 12.0, 12.1, and 12.999 will all become just 12.

You could do just use a double variable as soon as you read it:

an int expression on the RHS of = will automatically get promoted to a double on assignment.

Or you could cast your int variable to a double when the calculation time comes:


The output is wrong because if I input '10' then '0' for height of both parents it ouputs that the height of the male child will be 10 feet and 0 inches. The correct answer should be 10 feet and 5 inches roughly.



That may be a side effect of you losing precision in your int division. Fix that first and then see where you stand.
 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I see!
1. I stored the calculated height as a double
2. Changed the 13 to 13.0 to convert to double,
3. Type casted the height to an integer and did integer arithmetic to separate feet from inches.
4. Also declared inches as decimal format object and defined it as: height % 12.

 
reply
    Bookmark Topic Watch Topic
  • New Topic