Search code examples
javajava.util.scannerjoptionpane

Scanner to JOptionPane number (double) error


I am trying to convert a BMI calculator program from using scanner to using JOptPane's Though, when I bring it into the JOp's for some reason the numbers are getting really screwy. Testing with scanner mode I use 190 for weight and 70 for height which gives about 27.

However, when I bring this code into JOp's the same numbers yield about 1.36

Obviously this is way off, but I'm new to Java and I can't really figure out why.. The best idea I can come up with is that when I parse the strings into doubles it's taking some extra character formats and concatenating that with the ASCII value stored in the string changing the number completely.

"valueOf" and "parse" functions in the Double are both give the same results, which is what least me to this conclusion. Can anyone help?

import javax.swing.JOptionPane;

public class ComputeAndInterpretBMI

  {
    public static void main(String[] args)
    {

    /* //Original Code BloK --removed 22May2013;7892
    Scanner input = new Scanner(System.in);

    //prompt user to entier weight in #'s
    System.out.print("Enter weight in pounds: ");
    double weight = input.nextDouble();

    //prompt user to enter heigh in inches
    System.out.print("Enter hight in inches: ");
    double height = input.nextDouble();
    */

    //weight and height boxes
    String inputWeight = JOptionPane.showInputDialog ("Enter the weight in pounds");
    String inputHeight = JOptionPane.showInputDialog ("Enter the height in inches");
    //parse strings to doubles
    double weight = Double.valueOf(inputHeight);
    double height = Double.valueOf(inputWeight);

    final double KILOGRAMS_PER_POUND = 0.453359237; //THIS IS A CONST, NO TOUCH
    final double METERS_PER_INCH = 0.0254; //ANOTHER CONST, STILL NO TOUCH
    double weightInKilograms = weight * KILOGRAMS_PER_POUND;
    double heightInMeters    = height * METERS_PER_INCH;
    double bmi = weightInKilograms / (heightInMeters * heightInMeters);

    //Display the results here

    /*//Original code BloK --removed 22May2013;7892
    System.out.println("BMI is " + bmi);
        if (bmi < 18.5)
            System.out.println("underweight");
        else if (bmi < 25 )
            System.out.println("normal");
        else if (bmi < 30 ) 
            System.out.println("overweight");
        else
            System.out.println("obease");
    */
   String results;    

    if (bmi < 18.5)
       results = "underweight";
    else if (bmi < 25 )
       results = "normal";
    else if (bmi < 30 ) 
       results = "overweight";
    else
       results = "obease";

    String output = "BMI is :" + bmi + "\n" + results;

    JOptionPane.showMessageDialog(null, output);
    }
  }

Solution

  • You are assigning the values to the wrong variables after input; weight = height and visa versa:

    // Your code:
    double weight = Double.valueOf(inputHeight);
    double height = Double.valueOf(inputWeight);
    
    // Corrected code:
    double weight = Double.valueOf(inputWeight);
    double height = Double.valueOf(inputHeight);