Reputation: 27
Basically the task is : I should input a temperature value followed by a blank and the letter C for Celsius or F for Fahrenheit, for example: "-12 C" or "165 F" and the output should be what state water is in at the entered temperature. The melting point of ice is for this exercise at 32 F and the boiling point of water is at 212 F. I am stuck on how to get the input of Celcius OR Fahrenheit and how to update the code below of mine to suit the boiling points of the Fahrenheit as well. My code so far:
public static void stateOfWater(){
System.out.println("Please enter a temperature value in Celcius or Fahrenheit : ");
Scanner input = new Scanner (System.in);
int x = input.nextInt();
if (x <= 0)
System.out.println("Water is solid at " + x );
else if (100 <= x )
System.out.println("Water is gaseous at " + x );
else
System.out.println("Water is liquid at " + x );
input.close();
}
Thank you :).
Upvotes: 0
Views: 2949
Reputation: 1006
Here is one solution
public static void stateOfWater() {
System.out.println("Please enter a temperature value in Celcius or Fahrenheit : ");
Scanner s = new Scanner(System.in);
String[] argv = s.nextLine().split(" ");
int temp = Integer.parseInt(argv[0]);
char tempType = argv[1].charAt(0);
switch (tempType) {
case 'c':
if (temp <= 0) {
System.out.println("CELCIUS: Water is solid at " + temp);
} else if (temp >= 100) {
System.out.println("CELCIUS: Water is gaseous at " + temp);
} else {
System.out.println("CELCIUS: Water is liquid at " + temp);
}
break;
case 'f':
if (temp <= 32) {
System.out.println("FAHRENHEIT: Water is solid at " + temp);
} else if (temp >= 212) {
System.out.println("FAHRENHEIT: Water is gaseous at " + temp);
} else {
System.out.println("FAHRENHEIT: Water is liquid at " + temp);
}
break;
}
}
There is a specific format with the input, although you can change it to your liking.
input:
55 c
output:
CELCIUS: Water is liquid at 55
Here you can see where we split the string using a delimiter and assign the temperature type, and the temperature itself.
String[] argv = s.nextLine().split(" ");
int temp = Integer.parseInt(argv[0]);
char tempType = argv[1].charAt(0);
Upvotes: 1
Reputation: 8575
Try this:
//Get the input from user
String inputStr = input.nextLine();
//Find the space
int indexOfSpace = inputStr.lastIndexOf(" ");
//Read the temperature value
int x = Integer.valueOf(inputStr.substring(0, indexOfSpace));
//figure out the scale being used
String scale = inputStr.substring(indexOfSpace+1);
if(scale.equalsIgnoreCase("C")){
//handle Celcius input
} else if(scale.equalsIgnoreCase("F")){
//handle Fahrenheit input
} else{
throw new IllegalArgumentException();
}
Upvotes: 0
Reputation: 3166
You cal always get the last character and check whether that's 'C' or 'F'
string.substring(string.length() - 1)
and then cast the rest of the string into an int variable
Hope that helps.
Upvotes: 1
Reputation: 734
You need to detect whether the input is Celsius or Fahrenheit. One way is:
String str = input.nextString();
if (str.contains("F") || str.contains("f") {
//It's fahrenheit. process accordingly.
} else {
//default to celsius. process accordingly.
}
This code will fall back to Celsius if it doesn't detect anything.
Upvotes: 1