I want to check if the input I entered is the correct data type. For example if the user enters an int when I want them to enter a double then the program tells them there is an error. This is what I have so far:
System.out.println("Enter the temperature in double:");
String temp = input.nextLine();
try
{
Double temperature = Double.parseDouble(temp);
}
catch(Exception e)
{
isValid = false;
System.out.println("Temperature must be a double ");
}
All its doing is continuing on through the program and not printing out the error message when I enter an int. Been stuck on this for a while so any help would be appreciated.
intis a validdouble, so why would you expect it to fail?intis also adouble! You have to explicitely test against intness, too.