3 Replies - 24246 Views - Last Post: 08 February 2012 - 07:14 PM Rate Topic: -----

#1 sara1_2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-February 12

Convert English unit to metric

Posted 08 February 2012 - 06:21 PM

program to convert between units, particularly English units to Metric units . Write a program that can convert six specific English units (pt, gal, ft, mi, oz, lb) into six specific Metric units (ml, l, m, km, g, kg). Valid conversions: 1) pt, gal -> ml, l; 2) ft, mi -> m, km; 3) oz, lb -> g, kg. program should tell the user if they try to do an improper conversion (gal -> km for instance). Users can only enter units using the abbreviations above (i.e. inputting "Gallon" is incorrect -). Your final Metric value should be rounded to four decimal places.

The conversion rates are:

1 US gallon = 3.78541178 liters

1 foot = 0.3048 meters

1 ounce = 28.3495231 grams

INPUT: an English unit, a Metric unit, and the value that you want converted
OUTPUT: a statement indicating the initial value and unit and the new value and new unit


import java.util.Scanner;
import java.text.DecimalFormat;

public class UnitConvert1 {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);
String unit;

System.out.print("Convert from English unit? ");
unit = keyboard.nextLine();

String input;
char answer;

System.out.print("Convert to Metric unit? ");
input = keyboard.nextLine();
answer = input.charAt(0);

int gallon, foot, ounce, pint, pounds, grams, kilometers, miles, liters;

System.out.print("Amount? ");
gallon = keyboard.nextInt();


if (unit.equals("gal") && unit.equals("l")) {

}

double measurement = (gallon * 3.78541178);
DecimalFormat formatter = new DecimalFormat("#0.0000");
System.out.println((gallon)
+ (" gal = " + (formatter.format(measurement) + (" l"))));

if (unit.equals("ft") && unit.equals("m")) {

DecimalFormat formatter1 = new DecimalFormat("#0.0000");
double measurement1 = (foot * 0.3048);

System.out.println((foot)
+ (" ft = " + (formatter1.format(measurement1) + (" m"))));

}

if (unit.equals("oz") && unit.equals("g")) {
DecimalFormat formatter2 = new DecimalFormat("#0.0000");
double measurement2 = (ounce * 28.3495231);

System.out.println((ounce)
+ (" oz = " + (formatter2.format(measurement2) + (" g"))));
}

if (unit.equals("gal") && unit.equals("ml")) {

DecimalFormat formatter3 = new DecimalFormat("#0.0000");
double measurement3 = (gallon * 3785.41178);
System.out
.println((gallon)
+ (" gal = " + (formatter3.format(measurement3) + (" ml"))));


I'm not sure what is wrong? Can you please help me? Thanks :)

This post has been edited by Atli: 08 February 2012 - 06:23 PM
Reason for edit:: Use [code] tags when posting code!


Is This A Good Question/Topic? 0
  • +

Replies To: Convert English unit to metric

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Convert English unit to metric

Posted 08 February 2012 - 06:28 PM

First of all: Indentation. This is an essential skill for a programmer. If you don't indent your code properly, it will look... well, like the code you just posted: virtually unreadable.

Quote

I'm not sure what is wrong? Can you please help me?

You need to do better than that. How can we help you solve a problem if we don't know what the problem is?

What exactly is your program doing (or not doing) that is shouldn't be? Are you getting any error messages? Is there anything else you can tell us that can help pinpoint the issue?
Was This Post Helpful? 0
  • +
  • -

#3 sara1_2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-February 12

Re: Convert English unit to metric

Posted 08 February 2012 - 06:36 PM

View PostAtli, on 08 February 2012 - 06:28 PM, said:

First of all: Indentation. This is an essential skill for a programmer. If you don't indent your code properly, it will look... well, like the code you just posted: virtually unreadable.

Quote

I'm not sure what is wrong? Can you please help me?

You need to do better than that. How can we help you solve a problem if we don't know what the problem is?

What exactly is your program doing (or not doing) that is shouldn't be? Are you getting any error messages? Is there anything else you can tell us that can help pinpoint the issue?



well, when I try to compile it it won't give the results of the conversion. How do I make it read a specific unit and do the formula according to the unit the user enters.
Convert from English unit? gal
Convert to Metric unit? l
Amount? 23
23.0 gal = 87.0645 l
Was This Post Helpful? 0
  • +
  • -

#4 sara1_2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-February 12

Re: Convert English unit to metric

Posted 08 February 2012 - 07:14 PM

I got it I had unit.equals instead of input.equals to read the input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1