Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Returning an array from a method to the main program
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
LOL123




PostPosted: Sat Apr 24, 2010 10:19 pm   Post subject: 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();
  }
}


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;
  }
}


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 Smile[/code]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Apr 24, 2010 10:55 pm   Post subject: 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
}


Or something like that.
LOL123




PostPosted: Sat Apr 24, 2010 11:32 pm   Post subject: 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




PostPosted: Sun Apr 25, 2010 1:43 am   Post subject: 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




PostPosted: Sun Apr 25, 2010 9:22 pm   Post subject: Re: RE:Returning an array from a method to the main program

LOL123 @ Sun Apr 25, 2010 12:32 pm wrote:
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




PostPosted: Sun Apr 25, 2010 10:16 pm   Post subject: 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




PostPosted: Mon Apr 26, 2010 2:07 pm   Post subject: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: