1

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.

Kailua Bum
  • 1,368
  • 7
  • 25
  • 40
Ron_ish
  • 111
  • 4
  • 12

7 Answers7

7

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4
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
Mac
  • 14,615
  • 9
  • 62
  • 80
  • 1
    @Ron_ish - as much as I appreciate the accept, I think it's rightfully Jon's - he got the same answer, and got it first (not that he actually needs the rep!) – Mac Jul 24 '11 at 12:32
4

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().

msw
  • 42,753
  • 9
  • 87
  • 112
3

You need to call your toCelsius(double fahr) method, like this:

System.out.println(fahrenheit + " degrees Fahrenheit is " + toCelsius(fahrenheit) + " degrees Celsius"); // Step 4
Bohemian
  • 412,405
  • 93
  • 575
  • 722
3

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.

Nivas
  • 18,126
  • 4
  • 62
  • 76
2

On a line marked step4 you have an undeclared variable named celsius. Should be fahrenheit perhaps?

Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
2

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.

atamanroman
  • 11,607
  • 7
  • 57
  • 81