I need to make this program pass by reference. How can I do that?

import java.io.*;
import java.util.*;

public class Convert
{
	static Scanner console = new Scanner(System.in);
	
public static void main(String[] args)
{
	int pounds;
	int lb;
	int oz;
	int totalOz;
	int lbs;
	int ozs;
	int totalOzs;
	
	
	//Calling the functions
	readData(pound);
	Convert(lb, oz, totalOz);
	displayResult(lbs, ozs, totalOzs);
		
}		


//The function that reads the data
public static double readData(int pounds)
{
	System.out.print("How many pounds? ");
	pounds = console.nextInt();
	
	return pounds;
}

//The function that converts pounds to ounces
public static double Convert(int pounds, double ounces, double totalounces)
{
	ounces = pounds / 16;
	totalounces = ounces + (pounds % 16);
	
	return totalounces;
}

//The void function to display the result
public static void displayResult(int pounds, double ounces, double totalounces)
{
	System.out.print(pounds + " pounds equal " + totalounces + " ounces.");
}
}

Sorry about not putting it in the code tags

First, make sure all code you post is in [code]

[/code] tags (there is a button to apply them to highlighted code in the post editor).

Second, you can't use primitives "by reference". Your function needs to return the result of the calculation. Objects passed as parameters can be modified by methods, but primitive datatypes cannot.

So you're saying there is no way of me passing the data from my readData method to my Calculate method?

Sure there is. Either capture that function value in a variable and then pass that to your calculate method

lbs = readData();

("pounds" isn't a parameter to that method, it's the value you want to return)
Alternately you can use the method call itself directly in the second call

Convert( readData(pound), oz, totalOz);

(though "totalounces" shouldn't be a parameter - it's the return value)

import java.io.*;
import java.util.*;

public class Convert
{
	static Scanner console = new Scanner(System.in);
	
public static void main(String[] args)
{
	int pound=0;
	int lb=0;
	int oz=0;
	int totalOz=0;
	int lbs=0;
	int ozs=0;
	int totalOzs=0;
	
	
	//Calling the functions
	Convert( readData(pound), oz, totalOz);
	displayResult(lbs, ozs, totalOzs);
		
}		


//The function that reads the data
public static int readData(int pounds)
{
	System.out.print("How many pounds? ");
	pounds = console.nextInt();
	
	return pounds;
}

//The function that converts pounds to ounces
public static double Convert(int pounds, double ounces,double totalounces)
{
	ounces = pounds / 16;
	totalounces = ounces + (pounds % 16);
	
	return totalounces;
}

//The void function to display the result
public static void displayResult(int pounds, double ounces, double totalounces)
{
	System.out.print(pounds + " pounds equal " + totalounces + " ounces.");
}
}

I'm confused but I tried to understand what you was saying...
I put "Convert( readData(pound), oz, totalOz);" instead of what I had before then I had to intialize all my declerations to zero so I dont get an error. When I run it it gives me "0 pounds equal 0.0 ounces." I'm guessing cause of my declerations.

And about "totalounces"; should I be putting "double totalounces" WITHIN my method cause if I get rid of it completely it gives me an error.

You have declared all of those variables, but you haven't given them any values. You set those variables to be the results of your calculations

lbs = readData();

And you need to change that method to take no parameter. Parameters are for giving data to a method. You have declared it like you expect the method to fill in the value for you.

I understand that I declared it to 0 but if I didn't I got an error. Is there any way around that?
Should I have given them values? I thought that's why I prompted the user when I ran the program.
And where do I put "lbs = readData"
I don't know! :confused:

I'm saying that you need to actually assign those variables the value you want them to have. If you want to store what "readData()" returns in the variable "lbs", then you have to use that statement lbs = readData(); . You have all zeros in your program because you haven't changed their values after you initialized them. Those methods you have written will not fill in the values of the parameters you have passed them.

I'd recommend re-reading all your notes on using methods and perhaps this as well: http://www.codeguru.com/java/tij/tij0036.shtml

Comments
Thank You so much for your help!
This article has been dead for over six months. Start a new discussion instead.