# Thread: My code for converting temperatures...

1. Member
Join Date
Jan 2011
Posts
4
Rep Power
0

## 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.

2. 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

3. Member
Join Date
Jan 2011
Posts
4
Rep Power
0
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.

4. Java 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,
Java Code:
`if(br.readLine() == "C")`

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

Java Code:
`if(br.readLine().equals("C"))`

See, what happens now?

Goldest

5. 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

6. Member
Join Date
Jan 2011
Posts
4
Rep Power
0

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

7. Member
Join Date
Jan 2011
Posts
4
Rep Power
0
JosAH

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

Regards,

8. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
6
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. :)
Java Code:
```>>> s="string"
>>> s=="string"
True
>>> s=10
>>> s == 10
True```
Last edited by JavaHater; 01-18-2011 at 02:50 AM.

9. For those curious, in Java, String is an object that is treated in some ways like a primitive, but it is still an object. == Compares the value of a primitive, or the memory location of an object. For instance:
Java Code:
```Object a = new Object();
Object b = new Object();
System.out.println(a == b); //false
b = a;
System.out.println(a == b); //true```
The == doesn't compare the value, but the location - a & b are only equal when they both point to the same location.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•