Temperature Converter Using Java

by | Apr 13, 2023 | Basic Coding, Coding, Java

Home » Coding » Temperature Converter Using Java

Introduction of the Project

Java is a popular programming language known for its versatility and wide range of applications. One common programming exercise for beginners is creating a temperature converter, which takes a temperature input in one unit and converts it to another. In this project tutorial, we will explore how to develop a temperature converter using Java.

A temperature converter is a simple program that allows users to input a temperature value in one unit, such as Celsius, Fahrenheit, or Kelvin, and then convert it to another unit based on a predefined conversion formula. For example, users may input a temperature value in Celsius, and the program will convert it to Fahrenheit or Kelvin, depending on the desired output.

To develop a temperature converter in Java, we will need to understand basic programming concepts, such as data types, input/output, and conditional statements. We will also need to define the conversion formulas for each temperature unit and implement them in our program. Additionally, we will create a user-friendly interface using the Swing module for inputting temperature values and displaying the converted result.

We will walk through the step-by-step process of developing a temperature converter using Java, starting from designing the user interface, implementing the conversion formulas, handling user input and output, and testing the program. By the end of this project tutorial, you will have a basic understanding of how to develop a temperature converter using Java and be able to apply the concepts learned to other programming projects. So, let’s dive in and start building our temperature converter!



1. To implement the right mathematics formula such that the temperature can be accurately converted from one unit to another.

2. To create a user interface with buttons and graphics for the user to interact with our temperature converter module.


Before you start building a temperature converter using Java, you will need to have the following prerequisites:

1. Basic Knowledge of Java: You should have a basic understanding of Java programming concepts, including data types, variables, operators, conditional statements (such as if-else statements), loops (such as for or while loops), and basic input/output operations (such as using the Scanner class).

2. Java Development Environment: You will need to have a Java Development Kit (JDK) installed on your computer, which includes the Java compiler (javac) and Java runtime environment (JRE). You can download the JDK from the official Oracle Java website and follow the installation instructions.

3. Integrated Development Environment (IDE): While not strictly necessary, it is recommended to use an Integrated Development Environment (IDE) for Java development, such as Eclipse, IntelliJ IDEA, or NetBeans. We have used IntelliJ IDEA for our project

4. Understanding of Temperature Units and Conversion Formulas: You should have a basic understanding of different temperature units, such as Celsius, Fahrenheit, and Kelvin, and the conversion formulas between them. For example, the formula to convert Celsius to Fahrenheit is:

F = (C * 9/5) + 32

where F is the temperature in Fahrenheit and

C is the temperature in Celsius.

Understanding these conversion formulas will be essential in implementing the logic for the temperature converter.

5. Familiarity with Graphic User Interface (GUI) Design: Java’s Swing library, which is used for building graphical user interfaces (GUIs), will be required to design a user-friendly interface for inputting temperature values and displaying the converted results in a visually appealing manner.

Source Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Main extends JFrame
JTextField tempinput, tempoutput;
ButtonListener buttonListener;
JLabel inputlabel,convertlabel,outputlabel,imglabel;
JComboBox comboBox1, comboBox2;
public Main()
Container c = getContentPane();
String s1[] = {"Celsius", "Fahrenheit", "Kelvin"};
comboBox1 = new JComboBox(s1);
String s2[] = {"Celsius", "Fahrenheit", "Kelvin"};
comboBox2 = new JComboBox(s2);
inputlabel=new JLabel("Temperature:");
inputlabel.setFont(new Font("arial",Font.BOLD,14));
convertlabel = new JLabel("Convert to: ");
convertlabel.setFont(new Font("arial",Font.BOLD,14));
tempinput = new JTextField(10);
tempoutput = new JTextField(10);
outputlabel = new JLabel("Output:");
outputlabel.setFont(new Font("arial",Font.BOLD,14));
JButton button =new JButton("Convert");
buttonListener = new ButtonListener();
imglabel = new JLabel("");
imglabel.setIcon(new ImageIcon("images.png"));
private class ButtonListener implements ActionListener
public void actionPerformed(ActionEvent e)
String temp = (String) comboBox1.getSelectedItem();
String tempConvert = (String) comboBox2.getSelectedItem();
if(temp.equals("Celsius") && tempConvert.equals("Fahrenheit")){
double c = Double.parseDouble(tempinput.getText());
double f = (double) (c*1.8+32);
else if (temp.equals("Celsius") && tempConvert.equals("Kelvin")) {
double c = Double.parseDouble(tempinput.getText());
double k = (double) (c+273);
else if (temp.equals("Celsius") && tempConvert.equals("Celsius")) {
double c = Double.parseDouble(tempinput.getText());
if(temp.equals("Fahrenheit") && tempConvert.equals("Celsius")) {
double f = Double.parseDouble(tempinput.getText());
double c = (double)((f - 32)*5/9);
else if(temp.equals("Fahrenheit") && tempConvert.equals("Kelvin")) {
double f = Double.parseDouble(tempinput.getText());
double k = (double)((f - 32)*5/9 + 273.15);
else if(temp.equals("Fahrenheit") && tempConvert.equals("Fahrenheit")) {
double f = Double.parseDouble(tempinput.getText());
if(temp.equals("Kelvin") && tempConvert.equals("Fahrenheit")) {
double k = Double.parseDouble(tempinput.getText());
double f = (double) ((k*(9/5))-459.67);
else if (temp.equals("Kelvin") && tempConvert.equals("Kelvin")) {
double k = Double.parseDouble(tempinput.getText());
else if (temp.equals("Kelvin") && tempConvert.equals("Celcius")) {
double k = Double.parseDouble(tempinput.getText());
double c = (double) (k-273);
public static void main(String[] args)
new Main();

Explanation of the Code

As per the defined objectives, we can break the code into 2 parts. One involves creating the GUI, and the other is the temperature conversion logic.

Let us look at the GUI first:

1. For the interface, we will create two text fields. One field to input a temperature in a certain unit, and the other text field will be an output showing the converted temperature in another unit.

2. We will create one button also using Swing. This Button will be the ‘Convert’ button that will allow users to convert temperature from one unit to another.

3. We will also have two combo boxes. Combo boxes are essentially drop-down lists where a user can select one option from several options. One combo box will allow the user to choose the unit of the input and the other combo box, the unit of the output to which the temperature will be converted.

Moving to the temperature conversion, we have applied the following rules:

1. If the input temperature is in Fahrenheit, we will use (f – 32)*(5/9) to convert to Celsius and (f – 32)*(5/9) + 273.15) to convert to Kelvin.

2. If the input temperature is in Celsius, we will use (c*1.8 + 32) to convert to Fahrenheit and (c + 273) to convert to Kelvin.

3. If the input temperature is in Kelvin, we will use [(k*(9/5)) – 459.67] to convert to Fahrenheit and (k-273) to convert to Celsius.

4. If the conversion unit is the same as the input unit, then we will simply print the input.


Celsius to Fahrenheit

Fahrenheit to Kelvin

Temperature Converter Using Java - Fahrenheit to Kelvin

We have successfully built a Temperature converter using Java. This is a robust code with an easy-to-use graphic interface, as you can convert from any temperature unit to a different unit accurately using this Java application!


More Java Projects>>>

You May Also Like To Create…


Submit a Comment

Your email address will not be published. Required fields are marked *