# Simple Java Code Help--- temperature conversion using while loop

• February 26th, 2013, 10:20 AM
Idiot_willing_to_learn
Simple Java Code Help--- temperature conversion using while loop
Hello, extreme Java idiot here.

For lab, we had to have the program output to the console screen the conversion of 98.6 degrees Fahrenheit to celsius. I did that.
Then we had to have it print the conversion of Fahrenheit 0.0, 5.0, 10.0.... 40.0 using a for loop. Below, I think I did it.

Code :

```public class Lab3 { public static void main (String[] args){ float fahrenheit=98.6f, celsius; celsius=(5.0f/9)*(fahrenheit-32); System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius.");   System.out.println(); for (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){ celsius=(5.0f/9)*(fahrenheit-32); if (fahrenheit%5==0) System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius."); } } }```

After that, we had to use a while loop instead. Here is where i'm confused:
With while loop replacing for loop:

All I did was replace the if statement with a while one.

Code :

```public class Lab3 { public static void main (String[] args){ float fahrenheit=98.6f, celsius; celsius=(5.0f/9)*(fahrenheit-32); System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius.");   System.out.println(); for (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){ celsius=(5.0f/9)*(fahrenheit-32); while (fahrenheit%5==0) System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius."); } } }```

However, my program begins to run infinitely and does not stop when I do this.

But actually what the assignment says is that I have to replace the for loop with a while loop? So am I supposed to change the 'for
to a 'while?'

Thanks in advance for having patience with me.
• February 26th, 2013, 10:22 AM
KevinWorkman
Re: Simple Java Code Help--- temperature conversion using while loop
Quote:

Originally Posted by Idiot_willing_to_learn
But actually what the assignment says is that I have to replace the for loop with a while loop? So am I supposed to change the 'for
to a 'while?'

What happened when you tried exactly that?
• February 26th, 2013, 10:31 AM
Idiot_willing_to_learn
Re: Simple Java Code Help--- temperature conversion using while loop
Well if I just replace the 'for' with a 'while' it definitely wouldn't work.

Quote:

Lab3.java:8: error: ')' expected
while (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
^
Lab3.java:8: error: not a statement
while (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
^
Lab3.java:8: error: ';' expected
while (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
^
3 errors
If I want it to print out the temperatures 0.0, 5.0., 10.0, 15.0, ... 40.0, I'm guessing I definitely have to say while (fahrenheit%5==0). I'm not sure how to get the while loop to go up to 40 though.
• February 26th, 2013, 10:33 AM
KevinWorkman
Re: Simple Java Code Help--- temperature conversion using while loop