I have written this program and I have gotten the program to run but when I run the program and I try to run the input validation portion it does not go back into the loop. Also when I calculate celsius into farenheit my output comes out to 0.
Is there any way you can help me figure this problem out?

Thank You in Advance :lol:

/**********************************************
* DegreesConverter.java
*  
*
* This program converts either from degrees
* Celsius to Farenheit or degrees Farenheit
* to Celsius.
* *********************************************/

public class DegreesConverter
{
	public static void main (String [] args)

	{
	  double celsius;
	  double farenheit;
	  double temp;
	  char degrees;
               char response = 'Y';

	   System.out.print("Enter a temperature: ");
	   temp = Input.readDouble ();


         while (response == 'Y')
         {
	       System.out.print("Enter a C for Celsius or an F for Farenheit: ");
	       degrees = Input.readChar ();


	 if((degrees == 'C') || (degrees == 'c'))
              {
                  farenheit = 9/5 * (temp) + 32;
	     System.out.println("Temperature = " +temp + 'C');
	     System.out.println("Temperature in Farenheit = " +farenheit +'F');
	      }

	 else if ((degrees == 'F') || (degrees == 'f'))
	 {
	      celsius = 5/9 * (temp - 32);
	      System.out.println("Temperature = " +temp + 'F');
	      System.out.println("Temperature in Celsius = " +celsius + 'C');
	 }
	 else 
	 {
                   System.out.println("Invalid Entry");
                   //System.out.println("You entered" +degrees);
	      System.out.println("Enter a temperature followed by a C for       Celsius or F for Farenheit");
                   System.out.print("Enter a temperature: ");
                   temp = Input.readDouble ();
                   System.out.print("Enter a C for Celsius or an F for Farenheit: ");
                   degrees = Input.readChar ();


           }

	       System.out.print("Would you like to enter another temperature? Y or N: ");
	       response = Input.readChar ();
	       //System.out.print("Enter a temperature: ");
                   //temp = Input.readDouble ();




	 }
	} // End Main
 } // End Class DegreesConverter

Hi,
I think you need to do the following changes:
(5/9) should be (5.0/9.0) to be able to get double value and same for (9/5) should be (9.0/5.0).

In order for a loop to continue looping you need a condition inside the loop that tells it about the response. You only indicated that once outside the loop.

repeate this code before the closing bracket of the while loop.

 System.out.print("Would you like to enter another temperature? Y or N: ");
           response = Input.readChar ();

You don't need all this in the last else. If it your loop this time works it will go to the begnning of the loop and repeat all your statements.

   System.out.println("Enter a temperature followed by a C for       Celsius or F for Farenheit");
   System.out.print("Enter a temperature: ");
   temp = Input.readDouble ();
   System.out.print("Enter a C for Celsius or an F for Farenheit: ");
   degrees = Input.readChar ();}

Hope this helped.
Good luck

Edited 3 Years Ago by pyTony: fixed formating

the format of a while loop looks like this

int x=0; //or whatever the intial number u want
while (x<=100){
//wutever you want to do
x++;
}

so it needs to keep changing with something, otherwise, it is not a while loop

i guess the way you want the response = 'y' juss for keeping the program running, you can do in the Input.read thing, i mean whenever it reads, it runs the loop

This article has been dead for over six months. Start a new discussion instead.