Overclock
Overclock

Reputation: 73

Handing information from one file to another

Say I have two different files; one containing a bunch of methods that has an array within, and the other containing the code to save some data to a .txt file. How would I pass the information (in this case the array) from the first file to the second file in order for it to be written?

For example

public class loadsaMethods
{
   public static void main(String[] param)
   {
      howFast(); //arbitrary methods
      howSlow(); //that have some random uses
      canYouGo(); // e.g this calculates the speed of something
      myArray(); // this holds the data I want to write in the other file
   }
        /*assume code for other methods is here*/
   public static int[] myArray()
   {
      int[] scorep1 = new int[4];
      return new int[4];   // this array gets given values from one of the other methods
   }
}

The above code has an array in it that I wish to pluck

public class saveArray
{
   public static void main(String[] params) throws IOException 
   {
       PrintWriter outputStream = new PrintWriter(new FileWriter("mydata2.txt"));

       int NumberofNames = 3;
       outputStream.println(NumberofNames);

       String [] names = {"Paul", "Jo", "Mo"}; //this is the line that needs to contain the 
                                               //array but it doesn't know what values are 
                                               //stored in the array until the previous 
                                               //program has terminated
       for (int i = 0; i < names.length; i++)
       {
            outputStream.println(names[i]);
       }

       outputStream.close();

       System.exit(0);

   }
}

And this code wants to save the array values.

I'm just a little bit confused as to how one passes across the values that have just been determined by one program to a different program.

Upvotes: 0

Views: 30

Answers (1)

rewen
rewen

Reputation: 58

In your saveArray class you should call a method you've created in loadsaMethods class.

Try:

loadsaMethods data = new loadsaMethods();
int[] scorep1 = data.myArray();

Upvotes: 1

Related Questions