import java.util.Scanner;
public class TempConversion
{
public static void main(String []args)
{
Scanner scan = new Scanner(System.in);
String degSymbol = "\u00b0"; // unicode for the 'degree' symbol
do {
try {
System.out.print("\nEnter temperature or press q to exit: ");
String input = scan.next();
if (input.equals("q") || input.equals(" ")) {
System.exit(0);
} else {
double temp_input = Double.parseDouble(input);
System.out.printf("What unit of temp is %,.2f? " +
"\nCelsius\nFarenheit\nKelvin: ", temp_input);
char unit_from = scan.next().toUpperCase().charAt(0);
System.out.printf("\nConvert %,.2f%s%c to what unit? " +
"\nCelsius\nFarenheit\nKelvin: ",
temp_input, degSymbol, unit_from);
char unit_to = scan.next().toUpperCase().charAt(0);
String convert = Character.toString(unit_from) +
Character.toString(unit_to);
switch(convert) {
case "CF": //celsius to farenheit
double temp_results = (9.0/5.0) * temp_input + 32;
System.out.printf("\nRESULT: %,.2f\u00b0%c = %,.2f\u00b0F\n",
temp_input, unit_from, temp_results);
break;
case "FC": // farenheit to celsius
temp_results = (5.0/9.0) * (temp_input - 32);
System.out.printf("\nRESULT: %,.2f\u00b0%c = %,.2f\u00b0C\n",
temp_input, unit_from, temp_results);
break;
case "CK": //celsius to kelvin
temp_results = temp_input + 273.15;
System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sK\n", temp_input,
degSymbol, unit_from, temp_results, degSymbol);
break;
case "FK": // farenheit to kelvin
temp_results = (temp_input + 459.67) * 5.0/9.0;
System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sK\n", temp_input,
degSymbol, unit_from, temp_results, degSymbol);
break;
case "KF": // kelvin to farenheit
temp_results = temp_input * 9.0/5.0 - 459.67;;
System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sF\n", temp_input,
degSymbol, unit_from, temp_results, degSymbol);
break;
case "KC": // kelvin to celsius
temp_results = temp_input - 273.15;
System.out.printf("\nRESULT: %,.2f%s%c = %,.2f%sC\n", temp_input,
degSymbol, unit_from, temp_results, degSymbol);
break;
default:
System.out.println("\nERROR: One or more variables you entered " +
"are invalid. Please try again.");
break;
} // ends switch
} // ends else
} catch (NumberFormatException ignore) {
System.out.println("Invalid input.");
}
} while(true); //ends do
} // ends main
} // ends class