Here is the code I'm messing around with:
import java.util.*; public class Work {
public static void main(String[] args){
System.out.println("Type 1 to convert from Farenheit to Celsius. Type 2 to convert from Farenheit to Kelvin.");
Scanner sc = new Scanner(System.in);
double x = sc.nextDouble();
while ((x != 1) && (x != 2)){
System.out.println("Please enter a value of either 1 or 2");
x = sc.nextDouble();
}
if (x == 1){
System.out.println("Enter the degrees in F and it will be converted to C:");
double y = sc.nextDouble();
double ans = convertoC(y);
System.out.println(ans + " C");
} else if (x == 2){
System.out.println("Enter the degrees in F and it will be converted to K:");
double v = sc.nextDouble();
double ans = convertoK(v);
System.out.println(ans + " K");
}
}
public static double convertoK(double x){
return ((x + 459.67) * (5.0/9.0));
}
public static double convertoC(double x){
return ((x - 32.0) * (5.0/9.0));
}
}
Eclipse tells me there is a resource leak for the Scanner. How can I correct this? Also, is there a more efficient way to receive inputs then this? I feel like my implementation is very blocky and re-uses the same code over an over. Also, would I use a try/catch to allow a user to re-enter another input if they don't input a number (as it is, an exception simply gets thrown).
Thanks!