# Java Convert Fahrenheit Celsius Example

The sample Java program will convert between Fahrenheit and Celsius. If you enter a number followed by a C or c for Celsius, it will output the equivalent value in Fahrenheit. If you enter a number followed by a F or f for Fahrenheit, the program will output the equivalent value in Celsius. I added a very simple check that if no C,c,F,f, the program will prompt you for the proper format.

### Java Convert Fahrenheit Celsius Source Code

```package com.jcd.java.tutorials;

import java.util.Scanner;

public class JavaConvertTemp
{

public static void main(String[] args)
{

//Create a Scanner object
Scanner jcdIn = new Scanner(System.in);

System.out.println("Enter Temp To Convert as [32c or 90f]");

//Read input line as a String
String line = jcdIn.nextLine();

//If input contains a 'C' or 'c', convert to Fahrenheit
if(line.contains("c") || line.contains("C"))
{
String tmp = line.substring(0, line.length() - 1);
float temp = Float.parseFloat(tmp);
temp = (temp * 9)/5+32;
System.out.println("Temperatue in Fahrenheit: " + Math.round(temp));

}

//If input contains a 'F' or 'f', convert to Celsius
else if(line.contains("f") || line.contains("F"))
{
String tmp = line.substring(0, line.length() - 1);
float temp = Float.parseFloat(tmp);
temp = ((temp - 32)*5)/9;
System.out.println("Temperatue in Celsius: " + Math.round(temp));

}
else
{
System.out.println("Use Proper Format: 32c or 32C or 90f or 90F");
}

//Close the Scanner
jcdIn.close();

}

}
```

### Java Convert Fahrenheit Celsius Output

```Enter Temp To Convert as [32c or 90f]
32c
Temperatue in Fahrenheit: 90

Enter Temp To Convert as [32c or 90f]
90F
Temperatue in Celsius: 32

Enter Temp To Convert as [32c or 90f]
50f
Temperatue in Celsius: 10

Enter Temp To Convert as [32c or 90f]
20B
Use Proper Format: 32c or 32C or 90f or 90F

```