I am copying and pasting this code from a textbook into the onlinegdb compiler at https://www.onlinegdb.com/online_java_compiler, but I keep getting errors. The code, directly copied from the textbook, is
import java.util.Scanner;
public class TemperatureConverter {
public static double convertFahrenheitToCelsius(double temperatureF) {
double temperatureC = (temperatureF - 32) * 5 / 9;
return temperatureC;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your body temperature in Fahrenheit degrees: ");
double temperature = input.nextDouble();
temperature = convertFahrenheitToCelsius(temperature);
System.out.printf("Your body temperature in Celsius degrees is %f.%n", temperature);
if (temperature >= 37) {
System.out.println("You are ill!");
}
input.close();
}
}
1. If I put all the code in the "Main.java" file, I get the following error
class TemperatureConverter is public, should be declared in a file named TemperatureConverter.java
2. If I put the TemeperatureConverter class in a separate TemperatureConverter.java file which has only the convertFahrenheitToCelsius method and put all the rest (incl. public static void main(String[] args)) in a class Main in the Main.java file, then I get the error message
error: cannot find symbol
convertFahrenheitToCelsius(temperature);
^
What is wrong?! It drives me mad!