# Thread: Help with the calculation of a variable.

1. ## Help with the calculation of a variable.

Hi!
I just started learning java and got an assigment to create a program that calculates the BMI, and tells you one of the four options. The problem is that it always skips the first three options and gives the fourth every time.

Java Code:
```import javax.swing.JOptionPane;

public class CharDemo2 {

public static void main(String[] args) {

int pikkus;
int kaal;
double indeks;
String tulemus;
pikkus = Integer.parseInt(JOptionPane.showInputDialog("Sisesta enda pikkus:"));
kaal = 	Integer.parseInt(JOptionPane.showInputDialog("Sisesta enda kaal:"));

indeks = kaal / (pikkus * pikkus);

if (indeks >= 19 && indeks <= 25) {
tulemus = "Te olete normaalkaalus.";
} else if (indeks >= 25 && indeks <= 40) {
tulemus = "Te olete ülekaaluline.";
} else if (indeks > 40) {
tulemus = "Teie tervis on ohus.";
} else {
tulemus = "Te olete anorektik.";
}

JOptionPane.showMessageDialog( null, "Tulemus: " + tulemus);
}
}```

I believe the problem lies within the calculation of the variable "indeks", but I can't put my finger on it. Help is greatly appreciated.

2. put in System.out.println statements to check the value of all variables. Also println the result of the division to see if it is what you expect and to see if the program behaves correctly.

3. println shows that "pikkus" and "kaal" are inserted but the line

Java Code:
`indeks = kaal / (pikkus * pikkus);`
gives a result of 0.0

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
Originally Posted by rarschach
println shows that "pikkus" and "kaal" are inserted but the line

Java Code:
`indeks = kaal / (pikkus * pikkus);`
gives a result of 0.0
/ performs what is called "integer division" it always rounds the answer so that it is an integer. (The rounding is towards zero: so positive results get rounded down). The solution is to make one or both of the things you are dividing a double. That way you will get ordinary division.

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
The result is still a bit weird so you may want to check the formula. Isn't BMI weight/height? So why do you have pikkus*pikkus rather than just pikkus?

6. BMI is weight divided by height squared, so of pikkus is height in meters, the formula is set up fine. I still wonder if int division should matter too much here since BMI shouldn't be close to zero. To the original poster: are you using the correct units, meters and kg?

7. actually no. i never even thought about the units not matching, but indeed that's the problem. i tried to get the indeks by using cm's instead of meters. but meters mean that the pikkus value should be double and when i set it as double it doesent compile.

8. Originally Posted by rarschach
actually no. i never even thought about the units not matching, but indeed that's the problem. i tried to get the indeks by using cm's instead of meters. but meters mean that the pikkus value should be double and when i set it as double it doesent compile.
It worked for me, but I unfortunately have no way of knowing how or why it's not working for you given the information above. We'll be able to help you fix the compile problem a lot quicker if you post your code attempt and the error message as well as indicate the line that causes the error.

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
BMI is weight divided by height squared
Oh! Sorry about introducing the confusion.

10. I tried to get the double pikkus inserted using JOptionPane but I couldn't do it. So I tried this

Java Code:
```import java.util.Scanner;

public class CharDemo2 {

public static void main(String[] args) {

int kaal;
double indeks;
String tulemus;
Scanner scan = new Scanner(System.in);
System.out.println("Sisesta enda pikkus(meetrites): ");
double pikkus = scan.nextDouble();
System.out.println("Sisesta enda kaal(kilodes): ");
kaal = scan.nextInt();

indeks = kaal / ( pikkus * pikkus );
System.out.println(pikkus);
System.out.println(kaal);
System.out.println(indeks);

if ((indeks >= 19) && (indeks <= 25)) {
tulemus = "Te olete normaalkaalus.";
} else if ((indeks >= 25) && (indeks <= 40)) {
tulemus = "Te olete ülekaaluline.";
} else if (indeks > 40) {
tulemus = "Teie tervis on ohus.";
} else {
tulemus = "Te olete anorektik.";
}

System.out.println("Tulemus: " + tulemus);
}
}```
I still get an error when trying to insert the double pikkus - for example 1.8.

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at CharDemo2.main(CharDemo2.java:13)

//EDIT

It's working now. Thanks to everyone who replied :).
Last edited by rarschach; 01-24-2010 at 12:48 PM.

11. Originally Posted by pbrockway2
Oh! Sorry about introducing the confusion.
I never understood those funky formulas: height is some one dimensional magnitude while weight is something three dimensional. I would've expected height raised to the power three to make it somehow compatible or measurable with weight; but who am I? ;-)

kind regards,

Jos

12. You're comparing a rough estimate of the body's surface area with its volume. This division should be a one-dimensional entity that correlates with "girth". Regardless of it looking funny, it just so happens to scale well be the patient tall or short.

13. Originally Posted by Fubarable
You're comparing a rough estimate of the body's surface area with its volume. This division should be a one-dimensional entity that correlates with "girth". Regardless of it looking funny, it just so happens to scale well be the patient tall or short.
Ah, now I get it: the height squared is a rough estimation for the total skin area which is supposed to be an indication for the amount of fat in the three dimensional body. Why don't they measure the entire volume and compare it to the weight instead? Fat doesn't weigh much ... (this isn't really a question, just wondering).

kind regards,

Jos

14. Originally Posted by JosAH
Ah, now I get it: the height squared is a rough estimation for the total skin area which is supposed to be an indication for the amount of fat in the three dimensional body.
Not quite. The height squared won't change no matter how much fat you have. The weight over the height squared is an index of fat, something that is invariant no matter how tall you are.

Why don't they measure the entire volume and compare it to the weight instead? Fat doesn't weigh much ... (this isn't really a question, just wondering).
We're talking screening tools here where ease and speed trump accuracy. If you are tagged by a screening tool, then you should undergo a series of more thorough and accurate nutritional-state tests.

15. Originally Posted by Fubarable
Not quite. The height squared won't change no matter how much fat you have. The weight over the height squared is an index of fat, something that is invariant no matter how tall you are.
Yep, you're right; silly me. Except when all the fat piles up on your head ;-)

We're talking screening tools here where ease and speed trump accuracy. If you are tagged by a screening tool, then you should undergo a series of more thorough and accurate nutritional-state tests.
I find it a silly scale; any division of height divided by length would've done just as well imho. Or do they just want to keep their patients and beginner programmers busy? ;-)

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•