Results 1 to 15 of 15
  1. #1
    rarschach's Avatar
    rarschach is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    rarschach's Avatar
    rarschach is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

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

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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by rarschach View Post
    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. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #7
    rarschach's Avatar
    rarschach is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    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. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rarschach View Post
    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. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    BMI is weight divided by height squared
    Oh! Sorry about introducing the confusion.

  10. #10
    rarschach's Avatar
    rarschach is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    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. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    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. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    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. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    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. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    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

Similar Threads

  1. Need help with doing a calculation in Java
    By John D. in forum New To Java
    Replies: 6
    Last Post: 02-24-2009, 11:44 PM
  2. Delay on inputs during calculation
    By matt_well in forum New To Java
    Replies: 14
    Last Post: 07-26-2008, 04:17 PM
  3. Area Calculation: Add, Substract, XOR
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:08 PM
  4. Two problem for my rssi calculation program
    By iamchoilan in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2008, 03:49 PM
  5. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 02:42 PM

Posting Permissions

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