Unit Conversion Java Program

Unit Conversion Java Program | In this post, we will convert the units & discuss how to write the unit conversion Java program. The unit is a measurement that has a definite volume of a physical quantity which is adopted and defined by a convention of law.

These units are called SI units which is the International System of Units. For example, sugar is weighed by kg and water is weight by liters.

Unit Conversion Java Program Code

In this unit conversion Java program we will use BufferedReader class & InputStreamReader class to take input from the user. An object of BufferedReader class is taken as an instance variable so that it will be available to all instance/non-static methods.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Converter {
   BufferedReader br;

   public static void main(String[] args) throws Exception {
      Converter converter = new Converter();
      converter.br = 
       new BufferedReader(new InputStreamReader(System.in));

      int choice;
      boolean done = false;
      while (!done) {
         choice = converter.runMenu();
         switch (choice) {
         case 1:
            System.out.println("Convert Gallons To Liters");
            converter.convertGallonsToLtrs();
            break;
         case 2:
            System.out.println("Convert meters To miles");
            converter.convertMetersToMiles();
            break;
         case 3:
            System.out.println("Convert grams To carats");
            converter.convertGramsToCarats();
            break;
         case 4:
            System.out.println("Convert Inches To meters");
            converter.convertInchesToMtrs();
            break;
         case 5:
            System.out.println("Convert liters To Gallons");
            converter.convertLtrsToGallons();
            break;
         case 6:
            System.out.println("Convert grams To kilograms");
            converter.convertGmsTokgs();
            break;
         case 7:
            System.out.println("Convert Carats To Grams");
            converter.convertCaratsToGrams();
            break;
         default:
            System.out.println("Exited");
            done = true;
            break;
         }
      }
   }

   public int runMenu() throws Exception {
      int tableChoice;
      System.out.println("=========================");
      System.out.println("Conversion Table");

      System.out.println("1. Gallons To Liters");
      System.out.println("2. Meters To Miles");
      System.out.println("3. Grams To Carats");
      System.out.println("4. Inches To Meters");
      System.out.println("5. Liters To Gallons");
      System.out.println("6. Grams To kilograms");
      System.out.println("7. Carats To Grams");
      System.out.println("8. Quit");

      System.out.print("Enter choice: ");
      tableChoice = Integer.parseInt(br.readLine());
      while (tableChoice < 1 || tableChoice > 8) {
         System.out.print("Invalid choice, try again: ");
         tableChoice = Integer.parseInt(br.readLine());
      }
      return tableChoice;
   }

   public void convertGallonsToLtrs() throws Exception {
      System.out.print("Enter the Gallon: ");
      int gallon = Integer.parseInt(br.readLine());
      double liters = gallon * 3.7854118;
      System.out.println("Liters: " + liters);
   }

   public void convertMetersToMiles() throws Exception {
      System.out.print("Enter the Meters: ");
      int meter = Integer.parseInt(br.readLine());
      double miles = meter * 0.00062137119;
      System.out.println("Miles: " + miles);
   }

   public void convertGramsToCarats() throws Exception {
      System.out.print("Enter the Gram: ");
      int gram = Integer.parseInt(br.readLine());
      double carats = gram * 5;
      System.out.println("Carats: " + carats);
   }

   public void convertInchesToMtrs() throws Exception {
      System.out.print("Enter the Inches: ");
      int inches = Integer.parseInt(br.readLine());
      double meters = inches * 0.0254;
      System.out.println("Meters:" + meters);
   }

   public void convertLtrsToGallons() throws Exception {
      System.out.print("Enter the Liters: ");
      int liter = Integer.parseInt(br.readLine());
      double gallon = liter * 0.264172052358148;
      System.out.println("Gallons: " + gallon);
   }

   public void convertGmsTokgs() throws Exception {
      System.out.print("Enter the Grams: ");
      int gram = Integer.parseInt(br.readLine());
      double kilogram = gram * 0.001;
      System.out.println("kilogram: " + kilogram);
   }

   public void convertCaratsToGrams() throws Exception {
      System.out.print("Enter the Carat: ");
      int carat = Integer.parseInt(br.readLine());
      double gram = carat * 0.2;
      System.out.println("Gram:" + gram);
   }
}

Output:-

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 1
Convert Gallons To Liters
Enter the Gallon: 200
Liters: 757.08236

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 10
Invalid choice, try again: 12
Invalid choice, try again:

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 2
Convert meters To miles
Enter the Meters: 200
Miles: 0.12427423800000001

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 3
Convert grams To carats
Enter the Gram: 5000
Carats: 25000.0

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 4
Convert Inches To meters
Enter the Inches: 50
Meters:1.27

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 5
Convert liters To Gallons
Enter the Liters: 5000
Gallons: 1320.86026179074

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 6
Convert grams To kilograms
Enter the Grams: 50000
kilogram: 50.0

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 7
Convert Carats To Grams
Enter the Carat: 15
Gram:3.0

=========================
Conversion Table
1. Gallons To Liters
2. Meters To Miles
3. Grams To Carats
4. Inches To Meters
5. Liters To Gallons
6. Grams To kilograms
7. Carats To Grams
8. Quit
Enter choice: 8
Exited

The above unit conversion Java program runs continuously till we enter 8. On the input 8 for conversion table choice, the condition in the while loop will become false, and hence program execution will be completed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.