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);
}
}