0

So I am a beginner using java, and I am trying to have the user enter a temperature in Fahrenheit and convert it into Celsius, all while making sure the use enters a valid number for the double variable instead of letters. and than displaying in back to the user. I am particularly having trouble with the loop. Here is my code.

import java.util.Scanner;

public class TempConverter
{
    public static void main(String[] args) 
    {  
        Scanner in = new Scanner(System.in);
        double temp;
        do
        {
            System.out.print("Please enter temperature in Fahrenheit: ");
            while(!in.hasNextDouble())
            {
                System.out.println("ERROR. Please enter a valid temperature: ");
                in.nextDouble();
            }  
            temp = in.nextDouble();
        }
        double finalTemp = ((temp - 32)*5)/9;
        System.out.print("Celsius value of Fahrenheit value " + temp + " is " + finalTemp + ".");
    }
}
Alex
  • 1
  • 3
    Your syntax is all wrong. Go read https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html – azurefrog Apr 06 '16 at 20:38

1 Answers1

2

The while must come after. It will keep iterating until the condition in the while has been met.

    do
    {
        System.out.print("Please enter temperature in Fahrenheit: ");
            System.out.println("ERROR. Please enter a valid temperature: ");
            in.nextDouble();
        temp = in.nextDouble();
    }
    while(!in.hasNextDouble())
Aaronward
  • 109
  • 2
  • 11