tags:

views:

90

answers:

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


public class WeightConverter
{
    private double numOfLbs2Conv, numOfKilos2Conv, converted2Pounds, converted2Kilograms;
    private final double WEIGHT_CONVERSION_FACTOR = 2.20462262;
    private int desiredDecimalPlaces;
    private boolean toKilos, toPounds;

    public void readPoundsAndConvert()
    {
       toKilos = true;
        System.out.print("Enter the number of pounds to convert to " 
            + "kilograms: ");
        Scanner keyboard = new Scanner(System.in);
        numOfLbs2Conv = keyboard.nextDouble();
        converted2Pounds = numOfLbs2Conv / WEIGHT_CONVERSION_FACTOR;
    }


    public void readKilogramsAndConvert()
    {   
        toPounds = true;
        System.out.print("Enter the number of kilograms to convert to " 
            + "pounds: ");
        Scanner keyboard = new Scanner(System.in);
        numOfKilos2Conv = keyboard.nextDouble();
        converted2Kilograms = numOfKilos2Conv * WEIGHT_CONVERSION_FACTOR;
    }


    public void displayBothValues()
    {
        System.out.print("How many places after the decimal would you like? ");
        Scanner keyboard = new Scanner(System.in);
        desiredDecimalPlaces = keyboard.nextInt();

        String decimalCounter = "0.";
        for (int i = 0; i < desiredDecimalPlaces; i++)
        {
          decimalCounter = decimalCounter + "0";
        }

        DecimalFormat decimalsConverted = new DecimalFormat(decimalCounter);              


        if (toKilos)
        {

          System.out.println("The number of kilograms in " 
            + decimalsConverted.format(numOfLbs2Conv) + " pounds is " 
            + decimalsConverted.format(converted2Kilograms) + ".");
        System.out.print("Press Enter to continue ... ");
        System.out.println("");
        keyboard.nextLine();
        }


        if (toPounds) 
        {

        System.out.println("The number of pounds in "
            + decimalsConverted.format(numOfKilos2Conv) +  " kilograms is "
            + decimalsConverted.format(converted2Pounds) + ".");
        System.out.print("Press Enter to continue ... ");
        System.out.println("");
        keyboard.nextLine(); 
        }
    }
}    

Hi all.I'm having trouble getting this together. The output is screwed. If the user converts to pounds (readPoundsAndConvert()) first, the output will say that the converted answer is 0. If the user convert kilograms first, the kilograms will convert properly and then for somereason the readPoundsAndConvert() method will be called an d behave properly. I have no clue why this is happening and have been spending hours on it. Can someone tell me how to get this to behave properly? If you need me to post the rest of the program, I will.

A: 

how do you invoke the methods send all the prog

mohamed sakr
Typically, clarification questions are put on the question's comments.
BobbyShaftoe
I appreciate your help Mohamed. Here is the link to the full program http://www.2shared.com/file/h-A6vKof/homework.html
@BobbyShaftoe - He needs 48 more reputation to comment!
Ishtar
Especially now that his question has been downvoted twice.
Sam Dufel
+1  A: 

You're using your variables backwards... In readPoundsAndConvert() you're storing the converted value in converted2Pounds, but when you try to display it, you're reading from converted2Kilograms.

Sam Dufel
And you really should learn how to use a debugger. Go download eclipse learn to debug with it.
Sam Dufel
A: 

It looks like you're setting toKilos and toPounds to true in your two "convert" methods, but you aren't simultaneously setting the other to false. Thus, if you've called one of the convert methods before, when you call displayBothValues() both toKilos and toPounds will be true and both will be printed.

irrelephant