-5

I am creating a method called convertTemp(...) in java that takes a value , tempF,for the temperature in Fahrenheit as an input and convert the value into Centigrade (C) and displays its on screen. The formula for the conversion from Fahrenheit F to Centigrade C is C = 5/9*(F-32). The issue is I want to know what is my mistake of my code that cannot run in the eclipse.

package oop;

import java.util.Scanner;

public class convertTemp {

public static int temperature (int C , int F) {

     int Fahrenheit =5/9*(F-32);
    return Fahrenheit;
}
public static void main (String args []) {

    // TODO Auto-generated method stub
    int F ;

    int C ;

    Scanner sc = new Scanner(System.in);

    System.out.println(" The temperature of Fahrenheit(F) : ");

    F = sc.nextInt();

    System.out .println(" The temperature of Centigrade(C) : " + C );
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
oscar
  • 3
  • 1
  • 2
    What error are you getting? – Mureinik Dec 11 '17 at 17:09
  • 1
    Well, you're not calling your `temperature` method, for a start... – Jon Skeet Dec 11 '17 at 17:09
  • 1
    You never called your method. Also, your method takes 2 parameters but you are only using 1 of them which you may want to look into. – takendarkk Dec 11 '17 at 17:10
  • 1
    And your `temperature` method looks a bit confusing, in that it ignores the `C` value, and creates a `Fahrenheit` local variable from `F`. Is it meant to be a Celsius-to-Fahrenheit converter or vice versa? – Jon Skeet Dec 11 '17 at 17:10
  • 1
    Also `int Fahrenheit =5/9*(F-32);` will work differently than you'd expect. – QBrute Dec 11 '17 at 17:18
  • It may also be useful to read about java coding conventions. See oracle's article http://www.oracle.com/technetwork/java/codeconvtoc-136057.html – William Burnham Dec 11 '17 at 18:11

1 Answers1

1

There are a few problems with your code.

First of all you're not calling the method anywhere, so no code is executed there.
Secondly, I don't really know why your method temperature needs two parameters. Normally, you'd only need to enter a single value (in °F or °C) and get a result (in °C or °F).

Maybe you're looking for something like this:

public static double toCelsius(double fahrenheit) {
    return 5.0 / 9.0 * (fahrenheit - 32.0);
}

Note the use of the type double as well as the use of double literals. Otherwise you'll end up doing integer division which will give you wrong results.

To be able to do the reverse calculation, you'll need a second method:

public static double toFahrenheit(double celsius) {
    // do your logic
}

Then in your main class, you actually have to call those methods to see their effects.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double f = sc.nextDouble();
    double c = sc.nextDouble();

    double cResult = toCelsius(f);
    System.out.println(f + "°F is " + cResult + "°C");

    double fResult = toFahrenheit(c);
    System.out.println(c + "°C is " + fResult + "°F");
}
QBrute
  • 3,470
  • 6
  • 30
  • 36