-5

When I compile the code it tells me I have 2 errors, both variable may not of been initialized errors. Variables celsius and fahrenheit are the problems. I believe I have already initialized them in their respective methods.

import java.io.*;

class Converter
{

double celsius,fahrenheit,temperature,inFahrenheit,inCelsius;

double Celsius (double temperature)
    {
    celsius  = (5.0 / 9.0) * (temperature - 32);
    return celsius;
    }
double Fahrenheit (double temperature)
    {
    fahrenheit = (9.0 / 5.0) * temperature + 32;
    return fahrenheit;
    }
}



 public class ConverterTester
{
public static void main(String[] args)throws IOException
{
    double temperature,fahrenheit,celsius;

    InputStreamReader inStream = new InputStreamReader (System.in);
    BufferedReader stdin = new BufferedReader (inStream);

    String intemperature,inCelciusOrFahrenheit;

    System.out.println("What is the temperature");
    intemperature = stdin.readLine();
    temperature = Double.parseDouble(intemperature);

    System.out.println("What is the temperature you wish to convert to, Celsius or Fahrenheit");
    inCelciusOrFahrenheit = stdin.readLine();


if (inCelciusOrFahrenheit.equals("Celsius"))
    {
    Converter Conversion1 = new Converter();
    Conversion1.Celsius(celsius);
    System.out.println("Your new temperature is " + celsius);
    }

else if(inCelciusOrFahrenheit.equals("Fahrenheit"))
    {   
    Converter Conversion2 = new Converter();
    Conversion2.Fahrenheit(fahrenheit);
    System.out.println("Your new temperature is " + fahrenheit);
    }
else 
    {
    System.out.println("Please enter a correct temperature");
    System.exit(0);
    }
}
}       

The errors occur when I call the Celsius method and Fahrenheit method, I'm not sure if I'm allowed to use the variables when I call the methods. I however have not been able to find anything saying it is not allowed though.

Dks1
  • 11
  • 5
  • 2
    both `celcius` and `fahrenheit` never get assigned a value. – M. le Rutte Oct 15 '17 at 21:26
  • 2
    and you never use `temperature` – Bruno Delor Oct 15 '17 at 21:26
  • Where do you initialize `celcius` or `fahrenheit`? – Joe C Oct 15 '17 at 21:26
  • Why do you think equal variable names mean that they are _exactly_ the same? And your logic doesn't even make sense. This method call here: `Conversion1.Celsius(celsius)` for example. That uninitialized variable is passed as `temperature` into `Celsius` where you assume you initialze the same `celsius` variable using the one from `ConverterTester`. Thus you take an uninitialized variable to initialize itself? – Tom Oct 15 '17 at 21:28
  • 1
    Another problem you're about to run into: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – azurefrog Oct 15 '17 at 21:33

1 Answers1

0

Your only problem is that you didn't initialize temperature, celsius, or farenheit.
When making your variables here:
double celsius,fahrenheit,temperature,inFahrenheit,inCelsius;, you need to make temperature equal to something, say 20.
I would recommend to take out the ints celsius and farenheit, or set them equal to the doubles Celsius and Farenheit, that you set equal to temperature...math.

  • I see your point, however temperature needs to be equal to the user input(in this case the buffered reader stdin) did I just place that statement in the wrong place or totally misuse it? And the Celsius and Fahrenheit are supposed to be methods, not variables. – Dks1 Oct 15 '17 at 22:09
  • @Dks1 of course, I was merely pointing out what you need to do –  Oct 15 '17 at 22:16
  • @ ProgrammingNub ok thank you, I did not understand that. So my statement 'temperature =Double.ParseDouble(intemperature);' needs to be elsewhere? or set Celsius and Fahrenheit to temperature? – Dks1 Oct 15 '17 at 22:20