...

View Full Version : Sending arrays to method help



millertime3818
09-29-2011, 01:45 AM
I cannot figure out how to return the array back to the main method!
what am I doing wrong??


import java.util.*;

//lab 3

public class Prices
{
public static void main(String[] args)
{
double []array;
array = new double[10];

double fullPrice;

fullPrice = fillPrices(array);

System.out.println("testing");
}

public static double fillPrices(double []tenValues)
{
Scanner input = new Scanner(System.in);
double prices;


System.out.println("Please enter 10 prices: ");

for (int i = 0; i < 10; i++)
tenValues[i] = input.nextDouble();

return tenValues;
}
}

I thought you return an array by simply return (array name);

Am i missing something?

Thanks a bunch for whoever takes the time to help me!

Old Pedant
09-29-2011, 02:14 AM
This is the javaSCRIPT forum.

Your code is written in JAVA.

About the only thing the two languages have in common are the first four letters of their names.

***********

Having said that... this problem is obvious.



double fullPrice;

fullPrice = fillPrices(array);

If your fillPrices function returns an array, then *HOW* can you assign an array to a variable that is only one double value?

But, actually, there is no reason to return anything from your function: Since you create the array in main and passed it to the function, then when the function is done, the array created in main *will* be filled in.

In Java, arrays are passed by reference, not by value, so if you change their contents, you change them both in the function and in the caller of the function.

millertime3818
09-29-2011, 04:41 AM
alright, figured it out! thanks for the help and sorry for the ignorance...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum