Using methods improperly

• October 3rd, 2013, 10:41 PM
d0mlnance
Using methods improperly
I just recently started to learn about methods. I'm not that good with them nor do I fully understand them. This program I am writing is a simple program its asks the user if they want to convert pounds to kilograms or kilograms to pounds. In this assignment we are required to have 4 methods: The main method, P2k(pounds to kilos method), K2P, and a method that prints the results. I am not sure how to properly call the methods into the main method Here is my code if anyone can help me out.

Code Java:

```    package weightconversion;   import java.util.Scanner;   /** * * @author Zach */ public class WeightConversion {   //Create global conversion rate public static final double CONVERSION_RATE =2.204; /** * @param args the command line arguments */ public static void main(String[] args) { //Declare variables int nChoice=0; //the user will choose pounds or kilos. Scanner input = new Scanner (System.in); //Used to read input double dP2K=0; //Converted pounds to kilos double dK2P=0; //Converted kilos to pounds double dPounds=0; //variable for pounds double dKilos=0; //variable for kilos double dWeight=0; //Weight variable double dConvertedWeight=0; //variable for weight converted     //Display weight conversion System.out.println("WEIGHT CONVERSION"); //Displays pounds to kilos System.out.println("1." + "Pounds to kilograms"); //Displays kilos to pounds System.out.println("2." + "Kilograms to pounds");   //Prompt user to select type of conversion System.out.print("Please select the type of conversion" + " you would like to make:"); //Record input nChoice = input.nextInt();   //Prompt user to enter pounds if (nChoice == 1) { //Prompt user to enter pounds System.out.print("Please enter the pounds:"); //Record input dPounds = input.nextDouble(); //Calls method P2k with parameter dPounds dConvertedWeight = P2K(dPounds); } else if (nChoice == 2){ //Promp user to enter kilos System.out.print("Please enter the kilograms:"); //Record input dKilos = input.nextDouble(); //Calls method K2P with parameter dKilos dConvertedWeight = K2P (dKilos); } //What happens if user incorrectly enters wrong integer else //Tell user they need to run program again System.out.println("Error: Invalid conversion selection," + "Please run the program again. " ); dWeight = dPounds + dWeight; dWeight = dKilos + dWeight; }   //Converts pounds to kilograms public static double P2K(double dPounds){ double dConvertedPounds = 0; //The converted pounds dConvertedPounds = dPounds/CONVERSION_RATE; //Formula return dConvertedPounds; //Retrun statement }   public static double K2P(double dKilos){ double dConvertedKilos = 0; //The converted kilos dConvertedKilos = dKilos/CONVERSION_RATE; //Formula return dConvertedKilos; //Return statement   } public static void PrintResults(dConvertedWeight,nChoice,dWeight){   }             }```

Im getting an error code for the last method saying it can not find the symbol, I know i'm doing it incorrectly, but not sure how to properly write it.
• October 3rd, 2013, 10:53 PM
GoodbyeWorld
Re: Using methods improperly
public static void PrintResults(dConvertedWeight,nChoice,dWeight){

}

You forgot to put the variable types.

i.e.

public static void PrintResults(double dConvertedWeight, int nChoice, double dWeight)

Also, as for how to properly call a method inside the main method, you've already done it correctly here:

dConvertedWeight = P2K(dPounds);

dConvertedWeight = K2P (dKilos);
• October 3rd, 2013, 10:54 PM
Junky
Re: Using methods improperly
When you get an error you should copy and paste the exact message and indicate which line it occurs on (so we don't have to count).
• October 3rd, 2013, 11:24 PM
d0mlnance
Re: Using methods improperly
So i have the conversions working but I think im doing it without calling the printresults method could anyone tell me how i would incorporate the printresults method into my calculations, also when is displays it has to say 50 pounds but it says 50.0 pounds, should i change it to an int?

Code Java:

```    */ package weightconversion;   import java.util.Scanner;   /** * * @author Zach */ public class WeightConversion {   //Create global conversion rate public static final double CONVERSION_RATE =2.204; /** * @param args the command line arguments */ public static void main(String[] args) { //Declare variables int nChoice=0; //the user will choose pounds or kilos. Scanner input = new Scanner (System.in); //Used to read input double dP2K=0; //Converted pounds to kilos double dK2P=0; //Converted kilos to pounds double dPounds=0; //variable for pounds double dKilos=0; //variable for kilos double dWeight=0; //Weight variable double dConvertedWeight=0; //variable for weight converted     //Display weight conversion System.out.println("WEIGHT CONVERSION"); //Displays pounds to kilos System.out.println("1." + "Pounds to kilograms"); //Displays kilos to pounds System.out.println("2." + "Kilograms to pounds");   //Prompt user to select type of conversion System.out.print("Please select the type of conversion" + " you would like to make:"); //Record input nChoice = input.nextInt();   //Prompt user to enter pounds if (nChoice == 1) { //Prompt user to enter pounds System.out.print("Please enter the pounds:"); //Record input dPounds = input.nextDouble(); //Calls method P2k with parameter dPounds dConvertedWeight = P2K(dPounds); //Display calculation System.out.print(dPounds + " pounds is " + dConvertedWeight + " kilograms." ); } else if (nChoice == 2){ //Promp user to enter kilos System.out.print("Please enter the kilograms:"); //Record input dKilos = input.nextDouble(); //Calls method K2P with parameter dKilos dConvertedWeight = K2P (dKilos); //Display calculation System.out.print(dKilos + "kilograms is " + dConvertedWeight + " pounds. "); } //What happens if user incorrectly enters wrong integer else //Tell user they need to run program again System.out.println("Error: Invalid conversion selection," + "Please run the program again. " ); dWeight = dPounds + dWeight; dWeight = dKilos + dWeight;   if (nChoice == 1){   } }   //Converts pounds to kilograms public static double P2K(double dPounds){ double dConvertedPounds = 0; //The converted pounds dConvertedPounds = dPounds/CONVERSION_RATE; //Formula return dConvertedPounds; //Retrun statement }   public static double K2P(double dKilos){ double dConvertedKilos = 0; //The converted kilos dConvertedKilos = dKilos*CONVERSION_RATE; //Formula return dConvertedKilos; //Return statement   } public static void PrintResults(double dConvertedWeight,int nChoice, double dWeight){   }             }```

Here are the results

WEIGHT CONVERSION
1.Pounds to kilograms
2.Kilograms to pounds
Please select the type of conversion you would like to make:1
50.0 pounds is 22.686025408348456 kilograms.BUILD SUCCESSFUL (total time: 6 seconds)

Kilos to pounds :WEIGHT CONVERSION
1.Pounds to kilograms
2.Kilograms to pounds
Please select the type of conversion you would like to make:2
50.0kilograms is 110.2 pounds. BUILD SUCCESSFUL (total time: 3 seconds)

and the last one choosing neither :
WEIGHT CONVERSION
1.Pounds to kilograms
2.Kilograms to pounds
Please select the type of conversion you would like to make:-1
Error: Invalid conversion selection,Please run the program again.
BUILD SUCCESSFUL (total time: 2 seconds)
• October 3rd, 2013, 11:27 PM
Junky
Re: Using methods improperly
Currently you have the print statements in your if statement. Move them to the printResults method and then call that method instead.
• October 3rd, 2013, 11:45 PM
d0mlnance
Re: Using methods improperly
Okay so I moved it to the print method and called that method, but I messed up somehow because now when I convert something I says 0.0 pounds and does the conversion and repeats it twice.
Code Java:

```  * and open the template in the editor. */ package weightconversion;   import java.util.Scanner;   /** * This method converts kilograms to pounds or pounds to kilograms * @author Zach * @version 10/4/2013 */ public class WeightConversion { // This method prompts user to either select pounds or kilograms and then input a number which returns a conversion rate   //Create global conversion rate public static final double CONVERSION_RATE =2.204; /** * @param args the command line arguments */ public static void main(String[] args) { //Declare variables int nChoice=0; //the user will choose pounds or kilos. Scanner input = new Scanner (System.in); //Used to read input double dP2K=0; //Converted pounds to kilos double dK2P=0; //Converted kilos to pounds double dPounds=0; //variable for pounds double dKilos=0; //variable for kilos double dWeight=0; //Weight variable double dConvertedWeight=0; //variable for weight converted     //Display weight conversion System.out.println("WEIGHT CONVERSION"); //Displays pounds to kilos System.out.println("1." + "Pounds to kilograms"); //Displays kilos to pounds System.out.println("2." + "Kilograms to pounds");   //Prompt user to select type of conversion System.out.print("Please select the type of conversion" + " you would like to make:"); //Record input nChoice = input.nextInt();   //Prompt user to enter pounds if (nChoice == 1) { //Prompt user to enter pounds System.out.print("Please enter the pounds:"); //Record input dPounds = input.nextDouble(); //Calls method P2k with parameter dPounds dConvertedWeight = P2K(dPounds); //Calls method to print results PrintResults(dConvertedWeight,nChoice,dWeight);   } else if (nChoice == 2){ //Promp user to enter kilos System.out.print("Please enter the kilograms:"); //Record input dKilos = input.nextDouble(); //Calls method K2P with parameter dKilos dConvertedWeight = K2P (dKilos); //Calls method to print results PrintResults(dConvertedWeight,nChoice,dWeight);   } //What happens if user incorrectly enters wrong integer else //Tell user they need to run program again System.out.println("Error: Invalid conversion selection," + "Please run the program again. " ); dWeight = dPounds + dWeight; dWeight = dKilos + dWeight;   if (nChoice == 1){   } }   //Converts pounds to kilograms public static double P2K(double dPounds){ double dConvertedPounds = 0; //The converted pounds dConvertedPounds = dPounds/CONVERSION_RATE; //Formula return dConvertedPounds; //Retrun statement }   public static double K2P(double dKilos){ double dConvertedKilos = 0; //The converted kilos dConvertedKilos = dKilos*CONVERSION_RATE; //Formula return dConvertedKilos; //Return statement   } public static void PrintResults(double dConvertedWeight,int nChoice, double dWeight){ double dKilos=dWeight; double dPounds =dWeight; //Display calculation System.out.print(dPounds + " pounds is " + dConvertedWeight + " kilograms." ); //Display calculation //Display calculation System.out.print(dKilos + "kilograms is " + dConvertedWeight + " pounds. ");     } // end Method weightConversion             } // end Class WeightConversion```

These are the results :
WEIGHT CONVERSION
1.Pounds to kilograms
2.Kilograms to pounds
Please select the type of conversion you would like to make:1