
-----------------------------------
LOL123
Sat Apr 24, 2010 10:19 pm

Returning an array from a method to the main program
-----------------------------------
Hello for this program I need to read in 12 incomes from file in the main program then in a method I have to store all of the incomes in an array. I have done this but I dont know how to return an array

[code]
import java.io.*;
class arraymethod
{
  public static void main (String args[])
  throws java.io.IOException
  {
    
    FileReader fr = new FileReader ("incomes.txt");
    BufferedReader bfr = new BufferedReader (fr);
    array_Methods methodString = new array_Methods();
    
    final int MAX = 12;
    String input = " ";
    double list_incomes[] = new double[MAX];
    
    for (int counter = 0; counter < MAX; counter++)
    {
      input = bfr.readLine();
      methodString.initialize (input);
    }
      fr.close();
  }
}
[/code]

Here is my method...

[code]
class array_Methods
{
  public static String initialize (String input)
  {
    final int MAX = 12;
    double list_incomes[] = new double[MAX];
    
    for (int counter = 0; counter < MAX; counter++)
    {
      list_incomes [counter] = Double.parseDouble (input);
    }
    return list_incomes;
  }
}
[/code]

When I run the program I get this error
File: I:\usB\ICS\ArrayPrac 1 REDO\array_Methods.java  [line: 12]
Error: incompatible types
found   : double[]
required: java.lang.String

Thanks for checking :)[/code]

-----------------------------------
Insectoid
Sat Apr 24, 2010 10:55 pm

RE:Returning an array from a method to the main program
-----------------------------------
[code]
public static String[] whatever (Type input){
     String bleh[] = new String [x]
    return bleh
}
[/code]

Or something like that.

-----------------------------------
LOL123
Sat Apr 24, 2010 11:32 pm

RE:Returning an array from a method to the main program
-----------------------------------
Making the array a string wont work it needs to be a double. Since I am dealing with money in this program.

-----------------------------------
Barbarrosa
Sun Apr 25, 2010 1:43 am

Re: Returning an array from a method to the main program
-----------------------------------
All you've got wrong is your return type. Replace "String initialize" with "double[] initialize" or "Double[] initialize".

You're not going to get any output, though... you need System.out.print() or some kind of output stream for that.

-----------------------------------
wtd
Sun Apr 25, 2010 9:22 pm

Re: RE:Returning an array from a method to the main program
-----------------------------------
Making the array a string wont work it needs to be a double. Since I am dealing with money in this program.

Epic fail.

Money should never be modeled as a floating point number.  If you need further instruction, watch Office Space.

-----------------------------------
LOL123
Sun Apr 25, 2010 10:16 pm

RE:Returning an array from a method to the main program
-----------------------------------
Im sorry wtd, I didnt choose how the program needs to be completed Im just following instructions from my teacher.

-----------------------------------
DemonWasp
Mon Apr 26, 2010 2:07 pm

RE:Returning an array from a method to the main program
-----------------------------------
wtd is just referring to problems representing money with the limited floating-point representations we generally use. Instead, money is usually modeled with integers, with the units in cents.

However, for this assignment, the compile error you're getting is that the method's listed return type is "String", but you want it to be "double[]". Try changing the return type to "double[]" go from there.
