Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Question about methods, temperature conversion program

Hey everyone i want to start by saying i am very new to java so i had an issue i wanted to see if anyone can give me a solution. i basically have to make a program that displays a table that shows temperatures from celsius to fahrenheit from 40 degrees celsius down to 31 degrees celsius and convert them to fahrenheit on one side of the table, and on the other side of the table have 120 fahrenheit down to 30 degrees fahrenheit and convert them to celsius.

here is my code so far (i have not yet done the print line to display any of this in a table yet)

public class celsius{

public static void main(String[] args){

for(double celsius=40; celsius<=31.0; celsius--){
System.out.print(celsiusToFahrenheit(fahrenheit));
for(double fahrenheit=120; fahrenheit<=30.0; fahrenheit--){
System.out.print(fahrenheitToCelsius(celsius));
}
}
}
public static double celsiusToFahrenheit(double celsius){

return celsius=(5.0/9.0) * fahrenheit - 32;
}
public static double fahrenheitToCelsius(double fahrenheit){

return fahrenheit=(9.0/5) * celsius + 32;
}

}

i am getting a cannot find symbol error in line 7 i think it has something to do with not declaring the variable fahrenheit somewhere but i dont know where.

any help is appreciated  Reply With Quote

3. ## Re: Question about methods, temperature conversion program

this is how the program is supposed to print out when it is finished Screen Shot 2012-11-23 at 9.04.15 AM.png  Reply With Quote

4. ## Re: Question about methods, temperature conversion program

Suggestions:
• Don't use variables before declaring them. You are correct in that you're trying to use a variable, fahrenheit before it has been declared. The intro to Java tutorials will show you how to properly declare and use variables.
• Check your for loop conditions. Your first for loop will never loop since its condition, that celsius must be less than 31.0 will not be true when the loop begins.
• Plan out the logic of your program before committing it to code as this will save you most of the above grief.
• When posting code here, you'll want to wrap your code in code tags: [code] goes above your code block and [/code] goes below it. This will help your code retain its formatting and thus be readable.
• Keep trying. If you still get stuck, show us your latest code (with code tags), and ask your questions.  Reply With Quote

5. ## Re: Question about methods, temperature conversion program Originally Posted by curmudgeon Suggestions:
• Don't use variables before declaring them. You are correct in that you're trying to use a variable, fahrenheit before it has been declared. The intro to Java tutorials will show you how to properly declare and use variables.
• Check your for loop conditions. Your first for loop will never loop since its condition, that celsius must be less than 31.0 will not be true when the loop begins.
• Plan out the logic of your program before committing it to code as this will save you most of the above grief.
• When posting code here, you'll want to wrap your code in code tags: [code] goes above your code block and [/code] goes below it. This will help your code retain its formatting and thus be readable.
• Keep trying. If you still get stuck, show us your latest code (with code tags), and ask your questions.
thanks for the tips, check back later and i will update you  Reply With Quote

6. ## Re: Question about methods, temperature conversion program Originally Posted by aivory616 thanks for the tips, check back later and i will update you
Good. More specific tips, this line of code:

`for(double celsius=40; celsius<=31.0; celsius--) {`

states that you are creating a double variable named celsius, you are assigning it a value of 40, you state that the loop will continue to re-loop as long as celsius is less than or equal to 31 (which is impossible at the start of the loop since it starts at 40 -- maybe you want to change this condition), and you state that with each iteration of the loop, you will subtract 1 from celsius (that looks OK).

I suggest changing that condition that makes the loop not work. I also suggest that you make celsius an int, since for loops work better with ints in general. You can always convert it to double by casting when you want it treated as a double.

Then here:
`System.out.print(celsiusToFahrenheit(fahrenheit));`

You are saying, take a variable that doesn't exist and pass it into a method, and then print out the results of that method. Perhaps you don't want to use fahrenheit since the method name suggests that it converts a celsius value to a fahrenheit value. What temp would make more sense to pass into this method? celsius or fahrenheit? You already have a variable that you're looping with -- what does it hold? Can you use it instead?  Reply With Quote

7. ## Re: Question about methods, temperature conversion program

i appreciate your help heres my code so far

```public class celsius{

public static void main(String[] args){
double celsius = 40;
double fahrenheit = 120;

for(celsius=40; celsius>=31.0; celsius--){
System.out.print(celsiusToFahrenheit(fahrenheit));
for(fahrenheit=120; fahrenheit>=30.0; fahrenheit--){
System.out.print(fahrenheitToCelsius(celsius));
}
}

System.out.printf "Celsius             Fahrenheit | Fahrenheit                Celsius"+
"\n-------------------------------------------------------------------");
}

public static double celsiusToFahrenheit(double celsius){
double fahrenheit= 120;
return celsius=(5.0/9.0) * fahrenheit - 32;
}

public static double fahrenheitToCelsius(double fahrenheit){
double celsius= 40;
return fahrenheit=(9.0/5) * celsius + 32;
}

}```

it compiles now but does not print anything. By the end of my program i need it to look like this Screen Shot 2012-11-23 at 9.04.15 AM.png

and i am using double instead of int because i may have decimal numbers.  Reply With Quote

8. ## Re: Question about methods, temperature conversion program

The code you've posted won't compile. When you fix it so that it does compile, then you'll find that it does in fact print something. Your job is to fix it so that what it prints out makes sense. Are you allowed to use arrays?  Reply With Quote

9. ## Re: Question about methods, temperature conversion program

yeah we are allowed to use arrays we just started learning something. when i compiled it it would just print out a bunch of jibberish. i feel like i am on the right track i just cant seem to figure it out and i need to hand it in soon.  Reply With Quote

10. ## Re: Question about methods, temperature conversion program Originally Posted by aivory616 yeah we are allowed to use arrays we just started learning something. when i compiled it it would just print out a bunch of jibberish.
It's printing the values obtained from your method but without formatting

i feel like i am on the right track i just cant seem to figure it out and i need to hand it in soon.
• Consider creating an 2-dimensional array of double, meaning an array with 10 rows and 4 columns which should correspond to your desired output.
• Your for loops are currently nested one inside of the other. Don't do this as it doesn't make logical sense. I would use one for loop to calculate the first two columns of the double array, and then a second for loop to calculate the 2nd two columns of the double array.
• Then after filling the array, print out the result using a for loop.  Reply With Quote

11. ## Re: Question about methods, temperature conversion program

```public static void main(String[] args){

double fahrenheit = 120;
double celsius = 40;

System.out.printf ("Celsius             Fahrenheit | Fahrenheit                Celsius"+
"\n-------------------------------------------------------------------");

for(celsius=40; celsius>=31.0; celsius--){
System.out.print(celsiusToFahrenheit(fahrenheit));
}

for(fahrenheit=120; fahrenheit>=30.0; fahrenheit--){
System.out.print(fahrenheitToCelsius(celsius));
}

}

public static double celsiusToFahrenheit(double celsius){
double fahrenheit= 120;
return celsius=(5.0/9.0) * fahrenheit - 32;
}

public static double fahrenheitToCelsius(double fahrenheit){
double celsius= 40;
return fahrenheit=(9.0/5) * celsius + 32;
}

}```

i am going to try and avoid using an array because we learned this after this assignment was given and i think he wants us to solve it without it.  Reply With Quote

12. ## Re: Question about methods, temperature conversion program

Then you will want to use a single for loop that does for (int i = 0; i < 10; i++) and calculate the four numeric values inside of the loop, and prints out the row of data. You will need to reduce your celsius1 and your fahrenheit1 variables inside the loop and use this to calculate your fahrenheit2 and celsius2 results, then print out celsius1 fahrenheit2 fahrenheit1 celsius1 on each line, with proper formatting of course.  Reply With Quote