The user is asked to enter a temperature, then the user enters a number followed by either C or F for Celsius or Fahrenheit. The program is then to recognise the temp and what unit the temp is being measured in - using this data it tells your whether the "water" is solid, liquid or gaseous.
I've tried the below
public static void main(String[] args) {
Scanner myScan = new Scanner(System.in);
System.out.print("Enter THE temp: ");
String ent = myScan.next();
int temp = Integer.parseInt(ent.substring(0,4));
//char type = ent.charAt();
//System.out.println(temp);
// System.out.println(type);
if (temp >= 100) {
System.out.println("The water is gaseous");
} else if (temp < 0) {
System.out.println("The water is solid");
} else {
System.out.println("the water is liquid");
}
}
When I put the (0,4) in the temp substring, I would've thought that the output would be the first 3 numbers that are entered. Instead I get an error "java.lang.StringIndexOutOfBoundsException" when I enter 1 int or 3.
It looks like it is searching for a character that is out of bounds of the string. Instead of hard coding the string length to 4 use ent.length().
int temp = Integer.parseInt(ent.substring(0,ent.length()-1));
You may also need a formula to convert to Celsius if the last character is F
substring(ent.length()-1).equalsIgnoreCase("F");