File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problems with returning Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with returning Arrays" Watch "Problems with returning Arrays" New topic

Problems with returning Arrays

Pisces Iscariot

Joined: Sep 21, 2005
Posts: 4
Hi, I'm in a newbie Java class and I'm stuck on a really annoying problem.

Ok, here is my code. The code is receiving an array from DataWharehouse class (weight[]) and I have to calculate the elements into kilograms and send them to another class file (HealthRiskCalculatorTester).

public class HealthRiskCalculator {

public double kilosFromPounds() {
DataWharehouse data = new DataWharehouse();
double[] weight = data.getWeights();

double[] weightOutput = new double[10];
int i=0;

for (i=0; i<weight.length; i++) {
double weightInKilos = weight[i] * 0.4536; //calculating Kilos

weightOutput[i] = weightInKilos;

return weightOutput[i];}


**Below is a section in HealthRiskCalculatorTester, which is the one doing all the outputting for the above code.

public void testKilosFromPounds (HealthRiskCalculator calc) {

System.out.println("\nTesting kilosFromPounds");
// TODO: replace the following line with testing of calc.kilosFromPounds( )


Any help is greatly appreciated!
Henry Wong

Joined: Sep 28, 2004
Posts: 19336

Your kiloFromPounds() method is not returning an array, it is returning a double. As for the double, it is returning one of the array members, a member that you have never set.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pisces Iscariot

Joined: Sep 21, 2005
Posts: 4
Didn't I set the array element "i" when I did:
weightOutput[i] = weightInKilos; ?

If not, then how to I send a full array of results to HealthRiskCalculatorTester to be outputted?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 691
You don't say what the problem with your code is??
Does it compile ok? If not please copy the error messages here.

Does have an error when executing? Same as above.

Does it give an answer that you don't like? Please explain what the problem is.

One problem I see is in the following:
return weightOutput[i];

what is the value of i at this point?
rocket mass heater kickstarter
subject: Problems with returning Arrays