Java unit conversion program that convert (fl. oz, gal, oz, lb, in, ft, mi) to (ml, l, g, kg, mm, cm, m, km)
###### Posted by Samath
Last Updated: May 08, 2021

Write a java unit conversion program that asks the users from which unit they want to convert (fl. oz, gal, oz, lb, in, ft, mi) and to which unit they want to convert (ml, l, g, kg, mm, cm, m, km). Reject incompatible conversions (such as gal to km).

Example:

Convert from (fl.oz, gal, oz, lb, in, ft, mi): ft

Convert to (ml, l, g, kg, mm, cm, m, km): m

Enter value: 5.6
5.6000 ft = 1.7080 m

Code:

```import java.util.Scanner;

public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final double FLUID_OUNCE_TO_LITER = 0.029586;
final double GALLON_TO_LITER = 3.758;
final double OUNCE_TO_GRAM = 28.3495;
final double POUND_TO_GRAM = 453.6;
final double INCH_TO_METER = 0.00254;
final double FOOT_TO_METER = 0.305;
final double MILE_TO_METER = 1609.0;
System.out.print("Convert from (fl.oz, gal, oz, lb, in, ft, mi): ");
String convert_from = input.next();
System.out.print("Convert to (ml, l, g, kg, mm, cm, m, km): ");
String convert_to = input.next();
System.out.print("Enter value: ");
double value = input.nextDouble();
input.close();

double c_factor = 0.0;

if (convert_from.equals("fl.oz")) {
if (convert_to.equals("ml")) {
c_factor = FLUID_OUNCE_TO_LITER * 1000;
}
else if (convert_to.equals("l")) {
c_factor = FLUID_OUNCE_TO_LITER;
}
}
else if (convert_from.equals("gal")) {
if (convert_to.equals("ml")) {
c_factor = GALLON_TO_LITER * 1000;
}
else if (convert_to.equals("l")) {
c_factor = GALLON_TO_LITER;
}
}
else if (convert_from.equals("oz")) {
if (convert_to.equals("g")) {
c_factor = OUNCE_TO_GRAM;
}
else if (convert_to.equals("kg")) {
c_factor = OUNCE_TO_GRAM / 1000;
}
}
else if (convert_from.equals("lb")) {
if (convert_to.equals("g")) {
c_factor = POUND_TO_GRAM;
}
else if (convert_to.equals("kg")){
c_factor = POUND_TO_GRAM / 1000;
}
}
else if (convert_from.equals("in")) {
if (convert_to.equals("mm")) {
c_factor = INCH_TO_METER * 1000;
}
else if (convert_to.equals("cm")) {
c_factor = INCH_TO_METER * 100;
}
else if (convert_to.equals("m")) {
c_factor = INCH_TO_METER;
}
else if (convert_to.equals("km")) {
c_factor = INCH_TO_METER / 1000;
}
}
else if (convert_from.equals("ft")) {
if (convert_to.equals("mm")) {
c_factor = FOOT_TO_METER * 1000;
}
else if (convert_to.equals("cm")) {
c_factor = FOOT_TO_METER * 100;
}
else if (convert_to.equals("m")) {
c_factor = FOOT_TO_METER;
}
else if (convert_to.equals("km")) {
c_factor = FOOT_TO_METER / 1000;
}
}
else if (convert_from.equals("ml")) {
if (convert_to.equals("mm")) {
c_factor = MILE_TO_METER * 1000;
}
else if (convert_to.equals("cm")) {
c_factor = MILE_TO_METER * 100;
}
else if (convert_to.equals("m")) {
c_factor = MILE_TO_METER;
}
else if (convert_to.equals("km")) {
c_factor = MILE_TO_METER / 1000;
}
}

if (c_factor == 0.0) {
System.out.println("Conversion is unavailable.");
}
else {
double converted = value * c_factor;
System.out.printf("%.4f %s = %.4f %s", value, convert_from, converted, convert_to);
}
}

}

```
###### Binary to Decimal Conversion in C#
Samath | Jan 10, 2021
###### C program that reads parameter float (Vin) and a second parameter float (Vout ) and print the load resistor needed for this design with its unit
Samath | Jan 01, 2017
###### Convert Roman numerals to decimal using Java
Samath | Jan 17, 2024
###### Convert Integer to Roman Numeral using C#
Samath | Jan 20, 2024
###### C program that takes an 8-bit number and Convert it to hexadecimal format
Samath | Jan 01, 2017
###### C++ Convert from Decimal to Octal
Samath | Jan 05, 2017
###### C++ Convert an Integer to Roman Numeral
Samath | Jan 20, 2024
###### Python to Convert Decimal to Binary, Octal and Hexadecimal
gideonna | Apr 13, 2016
###### C program to convert temperature from degree Celsius to Fahrenheit
Samath | Jan 02, 2017
###### Convert ASCII Character to Hexadecimal and Binary using C++
Samath | Jan 05, 2017
###### Python Convert Decimal to Binary or Binary to Decimal
Samath | Jan 09, 2017
###### Java program that convert meters to miles, feet, and inches
Samath | Feb 08, 2021