Reputation: 111
Getting error with the following code:
import java.util.Scanner;
public class FahrenheitToCelsius{
public static void main(String[]args){
convertFToC();
}
/*gets input representing fahrenheit and displays celsius equivalent*/
public static void convertFToC(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter Fahrenheit temperature");
double fahrenheit = scan.nextInt();
System.out.println(fahrenheit + " degrees Fahrenheit is " +
fMethod(celsius) + " degrees Celsius");
}
/* calculates and returns the celsius equivalent */
public static double toCelsius(double fahr){
int BASE = 32;
double CONVERSION_FACTOR = 9.0/ 5.0;
double celsius = ((fahr-BASE)/(CONVERSION_FACTOR));
return celsius;
}
}
I get the following:
Error: celsius cannot be resolved to a variable
I need to use fMethod
to call the toCelsius
in the System.out.println
however i keep getting this error.
Upvotes: 1
Views: 1667
Reputation: 43497
You call fMethod
which doesn't exist and pass the value of celsius
when I think you meant fahrenheit
.
The variable celsius
is local to toCelsius()
and is therefore not resolvable inside convertFToC()
.
Upvotes: 4
Reputation: 11808
You are using celsius here:
System.out.println(fahrenheit + " degrees Fahrenheit is " + fMethod(celsius) + " degrees Celsius");
However, you did declare celsius only in toCelsius
and that's out of scope.
Upvotes: 2
Reputation: 18344
in the method convertFToC()
You get the fahrenheit
from the user and call fMethod(celsius)
You were supposed to call fMethod(
farenheit
)
More important:
If you read the compiler error message, you get not just the error, but the file name and the line number. If you go to the line number, you see a celcius
variable out of nowhere.
This is easy. I know asking on SO is easier, but you will never learn to read error messages and solve problems this way.
Learn to read and understand error messages. They are there for a reason.
Upvotes: 3
Reputation: 45586
On a line marked step4 you have an undeclared variable named celsius
. Should be fahrenheit
perhaps?
Upvotes: 2
Reputation: 14791
System.out.println(fahrenheit + " degrees Fahrenheit is " + fMethod(celsius) + " degrees Celsius"); //Step 4
should probably read
System.out.println(fahrenheit + " degrees Fahrenheit is " + toCelsius(fahrenheit) + " degrees Celsius"); //Step 4
Upvotes: 4
Reputation: 425083
You need to call your toCelsius(double fahr)
method, like this:
System.out.println(fahrenheit + " degrees Fahrenheit is " + toCelsius(fahrenheit) + " degrees Celsius"); // Step 4
Upvotes: 3
Reputation: 1500983
You haven't shown fMethod
at all, but it looks like you just want:
System.out.println(fahrenheit + " degrees Fahrenheit is " + toCelsius(fahrenheit)
+ " degrees Celsius");
You can't use celsius
in main
because it's a local variable in the toCelsius
method. Instead, you need to call that method and use the return value.
Upvotes: 7