aspose file tools*
The moose likes Beginning Java and the fly likes While loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While loops" Watch "While loops" New topic
Author

While loops

Collin Sampson
Greenhorn

Joined: Feb 27, 2012
Posts: 17
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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

Joined: Feb 27, 2012
Posts: 17
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

Joined: Jan 06, 2011
Posts: 441

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 27, 2012
Posts: 17
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: While loops