PDA

View Full Version : Resolved quick help with a simple program?


shugaray
02-22-2010, 12:07 AM
I'm new to coding and I'm taking a class in Java. My question is if anyone can help me with this program. It is a simple convert to celsius from farenheit but the farenheit f is a stored value ex 45. I am having tons of problems with it actually working because I get error messages :confused:. Here is the code:

public class ConvertertoC
{
public static void main (String[] args)
{
int f = 45;
char c= 5/9*(f-32);
System.out.println ("The centigrade for 45 is" + char c);
}
}

Thanks if you can help

cs_student
02-22-2010, 02:18 AM
Next time you post, please make sure you put your code within the code tags to ensure it keeps the formating. Also, if you get a compiler error, then please include that in your post. We are not compilers, and should not have to do it's job.

Unless you want an inaccurate conversion from Fahrenheit to Celsius, then I suggest you use a floating point variable type.

The error you are receiving from the compiler is from the code segment

System.out.println ("The centigrade for 45 is" + char c);


You already declared the variable, now you just need to pass it in as a parameter.

It should be

System.out.println ("The centigrade for 45 is" + c);


Also, is there a reason you are storing your number in a char primitive? While a number can be stored as a char, and vice versa, it is tradition to give the binary values of char's a different value than that of an int.

If you use the following code.

char askiiCode = 49;
System.out.println(askiiCode);


Should print out '1', because 49 is the number that corresponds to a '1' character.


cs_student

shugaray
02-22-2010, 01:06 PM
Thanks for the help, I'm new and my textbook isn't really user friendly for the new to programming. Idk why i used char I used float and double before that for accurate temps, but when I compiled it it said that was one of the errors but I must have misread it. I didn't know it was that simple of a fix by omitting char in the println stament.

Also sorry for not including error report with my question as well as using the code box thing which im new to this site and don't know how to so I just asked my question sorry for inconvience. But thanks for the help.