First of all, thanks to anybody who helps. Secondly, please keep in mind that I am a newbie (as shown by my code haha).
I'm just trying to get it to do the user input validation stuff. It's supposed to validate the air temperature, unit, and wind speed. No matter what I type in, it tells me that the unit is invalid. Did I do something wonky with the reading keyboard input part?
This is what my program outputs (sample user input is in bold):
- Wind Chill calculation program.
- Enter the air temperature followed by the unit. For example, 25 F for Fahrenheit or 25 C for Celsius.
- Air temperature: 25 F
- Enter the wind speed (in miles per hour).
- Wind speed: 10
- The unit is invalid.
- Wind Chill not calculated: Total Errors = 1
- Wind Chill program is completed.
- BUILD SUCCESSFUL (total time: 53 seconds)
Also, I haven't tried this part yet, but I want to make the unit case insensitive. I would do that with either .toLowerCase() or .toUpperCase(), right? I just want to make sure that I'm going the right direction.
The links below are the assignment requirements and a sample of what the program is supposed to do. You don't have to look at them if you don't want to (of course) but I added them just in case I'm not explaining myself well.
And here is what I have so far:
import java.util.Scanner;
public class WindChill2
{
public static void main(String args[])
{
// Variables
Scanner keyboard = new Scanner(System.in);
double temp, // Temperature
windSpeed, // Speed of wind in miles per hour
windChill; // Wind chill factor
int errorCount = 0; // User entry errors
char unit;
// Input
System.out.println("Wind Chill calculation program.");
System.out.println("Enter the air temperature followed by the unit. "
+ "For example, 25 F for Fahrenheit or 25 C for "
+ "Celsius.");
System.out.print("Air temperature: ");
temp = keyboard.nextDouble(); // Store user entry in temp
unit = keyboard.next().charAt(0); // Store unit (C or F)
System.out.println("Enter the wind speed (in miles per hour).");
System.out.print("Wind speed: ");
windSpeed = keyboard.nextDouble(); // Store user entry in windSpeed
// Processing
if (temp < -40 || temp > 50) // Validate temperature
{
System.out.println("The air temperature is invalid.");
errorCount++;
}
if (unit != 'C' || unit != 'F') // Validate unit
{
System.out.println("The unit is invalid.");
errorCount++;
}
if (windSpeed < 0 || windSpeed > 50) // Validate wind speed
{
System.out.println("The wind speed is invalid.");
errorCount++;
}
if (errorCount > 0) // Error counter
{
System.out.println("Wind Chill not calculated: Total Errors = "
+ errorCount);
}
else // Calculate wind chill factor
{
windChill = 91.4 - (91.4 - temp) * (.478 + .301 *
Math.sqrt(windSpeed) - .02 * windSpeed);
System.out.print("The Wind Chill factor is ");
System.out.printf("%.1f\n", windChill);
}
System.out.println("Wind Chill program is completed.");
}
}
You are writing the wrong condition for checking the unit. unit != 'C' || unit != 'F'
would return true when unit == 'F'
because one of the condition (unit != 'C'
) is true. OR
operator (||
) will return true if either one of the expression is true.
To fix that, simply use AND
operator (&&
), so that the unit is invalid if it does not equal to C
AND F
.
Therefore the condition should be unit != 'C' && unit != 'F'