# My code for converting temperatures...

• 01-17-2011, 04:26 PM
king4ever
My code for converting temperatures...
Hello All,

I just stayed around 6 hours to try and figure out how to write a code to convert Fahrenheit to Celsius. Here's my code:

import java.io.IOException;
public class fahrenheitcelsius {
public static void main (String[]args)
throws IOException
{
System.out.println("Enter the Temperature in Fehrnheit");
String degree;
int c = Integer.parseInt(degree);
c = (int) ((c-32)*(5)/(9));
System.out.println("The Temprature in Fahrenheit is \n"+c);
}
}

I wanna know if this is considered good for someone that doesn't have any programming background. Also I wanted to know If I can use an IF statement to make the program not only convert from Fahrenheit to Celsius but vice-versa. Anyways here's what I had in mind:

System.out.println("Enter the Temperature in Celsius");
String degree1;
int f = Integer.parseInt(degree1);
f = (int) (( f -32)*(0.55))
System.out.println("The Temperature in Celsius is \n" +f);
}else{
System.out.println("Enter the Temperature in Fahrenheit")
String degree;
int c = Integer.parseInt(degree);
c = (int) ((c*(9/5))+32)
System.out.println("The Temperature in Celsius is\n"+c);

But I couldn't get that to work , and because it's my first time on java ever I don't know if i'm really on the track or not.
• 01-17-2011, 04:42 PM
goldest
Quote:

Originally Posted by king4ever
But I couldn't get that to work

What really happens here? Errors? Code doesn't compile? Exceptions? Something else?

Goldest
• 01-17-2011, 04:45 PM
king4ever
it does execute but it let me enter values two times , and no matter what I wrote it always gives my the else part, it's like the IF doesn't exist.
• 01-17-2011, 04:54 PM
goldest
Code:

`if(br.readLine() == C)`
What is the C above? It's a String, right? Do we use Strings like this? Or we use double quotes " "? Like,
Code:

`if(br.readLine() == "C")`

Better way go for the equals method rather than == when you are comparing the Strings. Something like,

Code:

`if(br.readLine().equals("C"))`

See, what happens now?

Goldest
• 01-17-2011, 04:57 PM
JosAH
Quote:

Originally Posted by king4ever

That same mistake made ever again: Strings can't be compared with the == operator, use the String.equals("C") method call instead.

kind regards,

Jos
• 01-17-2011, 05:09 PM
king4ever

All it took is to change the == to equals , and put double quotes on the C.

well, I guess Java is not that bad after all. :p
• 01-17-2011, 05:17 PM
king4ever
JosAH

Thanks for your advice, I'll try and remember that next time I'm doing something.

Regards,
• 01-18-2011, 02:47 AM
JavaHater
Quote:

Originally Posted by king4ever

All it took is to change the == to equals , and put double quotes on the C.

well, I guess Java is not that bad after all. :p

you might also note that Java is one language that does not use "==" to compare strings. This is not so for other languages. For example, in Python, "==" is both used for comparing strings as well as numbers. Java is not that bad, but its not good enough. :)
Code:

```>>> s="string" >>> s=="string" True >>> s=10 >>> s == 10 True```
• 01-18-2011, 05:27 AM
```Object a = new Object(); Object b = new Object(); System.out.println(a == b); //false b = a; System.out.println(a == b); //true```