So I am a beginner using java, and I am trying to have the user enter a temperature in Fahrenheit and convert it into Celsius, all while making sure the use enters a valid number for the double variable instead of letters. and than displaying in back to the user. I am particularly having trouble with the loop. Here is my code.
import java.util.Scanner;
public class TempConverter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double temp;
do
{
System.out.print("Please enter temperature in Fahrenheit: ");
while(!in.hasNextDouble())
{
System.out.println("ERROR. Please enter a valid temperature: ");
in.nextDouble();
}
temp = in.nextDouble();
}
double finalTemp = ((temp - 32)*5)/9;
System.out.print("Celsius value of Fahrenheit value " + temp + " is " + finalTemp + ".");
}
}