Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why can't I call a method in Java?

0 votes
asked Jul 20, 2019 by Vladimir

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!

1 Answer

0 votes
answered Jul 23, 2019 by Monkey
Remove public in front of your class name and try again.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...