Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with returning Arrays

 
Pisces Iscariot
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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( )
System.out.println(calc.kilosFromPounds());

}


Any help is greatly appreciated!
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Pisces Iscariot
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 886
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic