1

I am writing a program to calculate some basic values like temperature, pressure, and density. It starts off by getting user input for the variables temperature, pressure, height, and density. These are stored as strings, due to the fact that users need the option to input the value "unknown" if the value is unknown.

System.out.println("What is the height in meters?");
height = reader.next();

System.out.println("What is the temperature in degrees celsius?");
temperature = reader.next();

System.out.println("What is the pressure in kPa?");
pressure = reader.next();

System.out.println("What is the density in kg/m^3?");
density = reader.next();

Then, there is another variable asking what the user wants to calculate.

System.out.println("What would you like to find? (Enter a number)");
System.out.println("1) Temperature");
System.out.println("2) Pressure");
System.out.println("3) Density");
whatToFind = reader.next();

After that, I have a switch statement to calculate whichever value the user chose. Based on the input value, it either activates findTemp(), findPressure(), or findDensity() and prints out the final value. I tried to use this and somewhere along the way, I got this error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "unknown"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at calculator.AerospaceCalculator.findTemp(AerospaceCalculator.java:73)
    at calculator.AerospaceCalculator.main(AerospaceCalculator.java:44)

The full AerospaceCalculator.java file can be found here.
The log can be found here. I put quotes around the input.

Please help me figure out what I did wrong. Thanks.

Here is are the methods "findTemp()", "findPressure()", and "findDensity()" in respective order.

public static void findTemp() {

    if(height == "unknown"){
        System.out.println("You cannot find temperature without height.");
    } else {
        double temp = Double.parseDouble(height);
        double hite = Double.parseDouble(temperature);
        temp = 15.04 - (0.00649 * hite);
        temperature = Double.toString(hite);
    }

}

public static void findPressure() {

    if(temperature == "unknown"){
        findTemp();
    } else {
        double press = Double.parseDouble(pressure);
        double temp = Double.parseDouble(temperature);
        press = 101.29 * (Math.pow((temp + 273.1) / (288.08), 5.256));
        pressure = Double.toString(press);
    }

}

public static void findDensity() {

    if(pressure == "unknown") {
        findPressure();
    } else {
        double dens = Double.parseDouble(density);
        double press = Double.parseDouble(pressure);
        double temp = Double.parseDouble(temperature);
        dens = press / (0.2869 * (temp + 273.1));
        density = Double.toString(dens);
    }

}
Andrew Graber
  • 177
  • 4
  • 12
  • 2
    Post the relevant portions of code here in your question, not through links that potential problem solvers have to sort through. – takendarkk Sep 08 '14 at 18:44
  • Sorry, I just added it. – Andrew Graber Sep 08 '14 at 18:48
  • 1
    Side notes: Don't compare String with `==`, use String.equals() method instead. Also, I see you parsing `height` and giving it to `temp` and also parsing `temperature` and giving it to `hite`. Is that really what you wanted? – takendarkk Sep 08 '14 at 18:50
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – David Conrad Sep 08 '14 at 18:51
  • It looks like you might end up with a Double.parseDouble("unkown") for some cases. Which, I think could cause the error you are having. – 182764125216 Sep 08 '14 at 18:54
  • @Takendarkk I actually just noticed that and I switched them to the way they ought to be. Thanks. – Andrew Graber Sep 08 '14 at 18:58

2 Answers2

2

You're comparing the input value to a constant String by reference equality, like so:

if(height == "unknown"){

This is not the proper way to compare String objects in Java. Rather, you should use the equals() method like so:

if ("unknown".equals(height)) {

There is also the issue of checking your temperature variable to determine whether its value is set to "unknown". If you call Double.parseDouble() and pass it in a String value that is not numeric, an exception will be thrown, as you've encountered.

I would suggest proofreading/organizing some of your other sections of code as well, as you seem to have confusing/mismatched variable names, at least based on assumed intention:

double temp = Double.parseDouble(height);
double hite = Double.parseDouble(temperature);
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
0

Use reader.nextDouble() to read double form the user input.

Check the documentation here.

Cacho Santa
  • 6,846
  • 6
  • 41
  • 73