package tempconverter;
import java.util.*;
import java.util.Scanner;
public class TempConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double tem;
System.out.print("Enter number: ");
double temp = sc.nextDouble();
System.out.println("Convert to Celsius or Fahrenheit (C or F): ");
int input = sc.nextInt();
if (input == 'C'){
System.out.println("Fahrenheit to Celcius is: " + toCelsius(temp));
}else if(input == 'F'){
toFahrenheit(temp);
}
public static double toCelsius(double cels){
double far = 5/9.0*(cels-32);
return far;
}
public static void toFahrenheit(double fahr){
double tem = 9/5.0*fahr+32;
System.out.println("Celsius to Fahrenheit: " + toFahrenheit(tem));
}
}
I refactored your code. You were having a recursive call inside one of your methods and your method were not returning values.
public static void main(String[] args) {
String name = null;
float temperature;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperature: " );
temperature = in.nextFloat();
System.out.println("Convert to Celsius or Fahrenheit (C or F): ");
name = in.next();
if (name.equals("C")) {
System.out.println("Fahrenheit to Celcius: " + toCelsius(temperature));
}else if (name.equals("F")){
System.out.println("Celsius to Fahrenheit: " + toFahrenheit(temperature));
}
}
public static double toCelsius(double cels){
double far = 5/9.0*(cels-32);
return far;
}
public static double toFahrenheit(double fahr){
double tem = 9/5.0*fahr+32;
return tem;
}