How to Create a Temperature Converter in Java

Sheeraz Gul Feb 02, 2024
How to Create a Temperature Converter in Java

The temperature has three types of measuring units: Celsius, Fahrenheit, and Kelvin. This tutorial demonstrates how to convert temperature from one unit to another for all types of measuring units.

Create a Temperature Converter in Java

According to the unit, there can be six types of conversions from one unit to another. Each conversion has a different formula.

The table below demonstrates all the formulas for temperature conversion:

Conversion Formula
Celcius to Fahrenheit °F = (°C × 9/5) + 32
Celcius to Kelvin °K = °C + 273.15
Fahrenheit to Celcius °C = (°F − 32) × 5/9
Fahrenheit to Kelvin °K = (°F − 32) × 5/9 + 273.15
Kelvin to Celcius °C = °K − 273.15
Kelvin to Fahrenheit °F = (°K − 273.15) × 9/5 + 32

These six formulas can be implemented in Java to convert the temperature. We implemented all six formulas in one program to create a universal temperature converter.

See example:

package delftstack;

import java.util.*;
public class Temprature_Converter {
  static Scanner sc = new Scanner(System.in); // Scanner Class

  // Celcius to Fahrenheit
  static double Celcius_to_Fahrenheit(double Celcius) {
    double Fahrenheit = (Celcius * 9 / 5) + 32;
    return Fahrenheit;
  }
  // Celcius to Kelvin
  static double Celcius_to_Kelvin(double Celcius) {
    double Kelvin = Celcius + 273.15;
    return Kelvin;
  }

  // Fahrenheit to Celcius
  static double Fahrenheit_to_Celcius(double Fahrenheit) {
    double Celcius = (Fahrenheit - 32) * 5 / 9;
    return Celcius;
  }
  // Fahrenheit to Kelvin
  static double Fahrenheit_to_Kelvin(double Fahrenheit) {
    double Kelvin = (Fahrenheit - 32) * 5 / 9 + 273.15;
    return Kelvin;
  }

  // Kelvin to Celcius
  static double Kelvin_to_Celcius(double Kelvin) {
    double Celcius = Kelvin - 273.15;
    return Celcius;
  }
  // Kelvin to Fahrenheit
  static double Kelvin_to_Fahrenheit(double Kelvin) {
    double Fahrenheit = (Kelvin - 273.15) * 9 / 5 + 32;
    return Fahrenheit;
  }

  // Read the value of temperature given by the user
  static double Input_Temperature(String Temperature) {
    System.out.println("Enter the " + Temperature + " value:");
    double val = sc.nextDouble();
    return val;
  }

  // Print converted value of temperature
  static void Output_Temperature(double Value, String Temperature) {
    System.out.println("The " + Temperature + " value is: " + Value);
  }

  // Driver Method
  public static void main(String args[]) {
    System.out.println("Enter 1 for Celcius to Fahrenheit\nEnter 2 for Celcius to Kelvin\n"
        + "Enter 3 for Fahrenheit to Celcius\nEnter 4 for Fahrenheit to Kelvin\n"
        + "Enter 5 for Kelvin to Celcius\nEnter 6 for Kelvin to Fahrenheit\n7. Exit");
    do {
      System.out.println("\nEnter Your Temperature Choice Number: ");
      int Temprature_Choice = sc.nextInt();
      double Choice_Number = 0;
      switch (Temprature_Choice) {
        case 1:
          Choice_Number = Input_Temperature("Celcius");
          Output_Temperature(Celcius_to_Fahrenheit(Choice_Number), "Fahrenheit");
          break;
        case 2:
          Choice_Number = Input_Temperature("Celcius");
          Output_Temperature(Celcius_to_Kelvin(Choice_Number), "Kelvin");
          break;
        case 3:
          Choice_Number = Input_Temperature("Fahrenheit");
          Output_Temperature(Fahrenheit_to_Celcius(Choice_Number), "Celcius");
          break;
        case 4:
          Choice_Number = Input_Temperature("Fahrenheit");
          Output_Temperature(Fahrenheit_to_Kelvin(Choice_Number), "Kelvin");
          break;
        case 5:
          Choice_Number = Input_Temperature("Kelvin");
          Output_Temperature(Kelvin_to_Celcius(Choice_Number), "Celcius");
          break;
        case 6:
          Choice_Number = Input_Temperature("Kelvin");
          Output_Temperature(Kelvin_to_Fahrenheit(Choice_Number), "Fahrenheit");
          break;
        case 7:
          System.exit(0);
          break;
        default:
          System.out.println("Invalid Input");
      }
    } while (true);
  }
}

The code above will take input from the user as a choice number and then the temperature to convert, then convert it based on the choice.

See output:

Enter 1 for Celcius to Fahrenheit
Enter 2 for Celcius to Kelvin
Enter 3 for Fahrenheit to Celcius
Enter 4 for Fahrenheit to Kelvin
Enter 5 for Kelvin to Celcius
Enter 6 for Kelvin to Fahrenheit
7. Exit

Enter Your Temperature Choice Number:
1
Enter the Celcius value:
50
The Fahrenheit value is: 122.0

Enter Your Temperature Choice Number:
2
Enter the Celcius value:
50
The Kelvin value is: 323.15

Enter Your Temperature Choice Number:
3
Enter the Fahrenheit value:
50
The Celcius value is: 10.0

Enter Your Temperature Choice Number:
4
Enter the Fahrenheit value:
50
The Kelvin value is: 283.15

Enter Your Temperature Choice Number:
5
Enter the Kelvin value:
50
The Celcius value is: -223.14999999999998

Enter Your Temperature Choice Number:
6
Enter the Kelvin value:
50
The Fahrenheit value is: -369.66999999999996

Enter Your Temperature Choice Number:
7
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook