1

I'm trying to make my program write to a file using two methods. It compiles with no errors, but when the file that it wrote to is opened, only coordinates with (0,0) are printed. Its supposed to print 10000 random coordinated from the array. Why does it only print out (0,0) coordinates? Also did I place my return statement in the correct place?

import java.io.*;

public class program
{
  public static void main (String [] args) throws IOException
  {

    int points = 10000, dimension = 2, lengthA = 100;//int variables are declared and initialized

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));

    double length [] = new double [dimension];//array for length is declared
    double coordinate [][] = new double [points][dimension];//coordinate array is declared

    for (int i = 0; i < points; i++){
      fileOut.println(java.util.Arrays.toString(coordinate[i]));
                      }
      fileOut.close();//writes to file

  }//end main method

    public static double writeTofile (double length[], double coordinate[][], int points, int dimension, int lengthA)
    {
      int x = 0, y = 0;
      for(int z = 0; z < dimension; z++){//fills the length array with the the set value of lengthA
      length[z] = lengthA;
      }

    for(x = 0; x < points; x++){//runs 1000 times to print 1000 data points
      for (y = 0; y < dimension; y++){//runs 2 times to print an x and y coordinate
        coordinate [x][y]= (2 *Math.random() - 1) * length[y];// finds a random number in the range and assiigns it to the coordinate array
      }//end for
    }//end for
    return coordinate[x][y];
  }//main method
}//program

I've changed my code and it compiles. however when you look at the file it printed to, random letters ([D@76f1fad1[D@889ec59...) are seen. Why is this happening?

import java.io.*;

public class program
{
  public static void main (String [] args) throws IOException
  {

    int points = 10000, dimension = 2, lengthA = 100;//int variables are declared and initialized

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));

    double length [] = new double [dimension];//array for length is declared
    double coordinate [][] = new double [points][dimension];//coordinate array is declared

    writeTofile(length, coordinate, points, dimension, lengthA);

    for (int i = 0; i < points; i++){
      fileOut.println(Arrays.toString(coordinate[i]));
                      }
      fileOut.close();//writes to file

  }//end main method

    public static void writeTofile (double length[], double coordinate[][], int points, int dimension, int lengthA)
    {
      int x = 0, y = 0;
      for(int z = 0; z < dimension; z++){//fills the length array with the the set value of lengthA
      length[z] = lengthA;
      }

    for(x = 0; x < points; x++){//runs 1000 times to print 1000 data points
      for (y = 0; y < dimension; y++){//runs 2 times to print an x and y coordinate
        coordinate [x][y]= (2 *Math.random() - 1) * length[y];// finds a random number in the range and assiigns it to the coordinate array
      }//end for
    }//end for
  }//main method
}//program
1
  • 1
    1. You don't have any calls to your method writeToFile(). 2. I think you don't fully understand the nature of two-dimensional arrays Commented Sep 23, 2013 at 21:32

1 Answer 1

3

You are not calling the method that fills the array of coordinates with values from the main method.

You can call it as:

...
double coordinate [][] = new double [points][dimension];//coordinate array is declared

writeTofile(length, coordinate, points, dimension, lengthA)

for (int i = 0; i < points; i++){
    ...

As for the return statement, it's in the right place, but it will throw an ArrayIndexOutOfBoundsException because it is accessing an offset in the array that does not exist. What do you want a return value for, what use do you have for it?

Sign up to request clarification or add additional context in comments.

7 Comments

when I remove the return, it tells me that i'm missing a return statement. And, i'm not clear on where I should call the method to fill the arrays. Would I place it in the main method or second method?
A method that does not return anything has to be declared with a return type void. As to where you should call the second method, see the example code in the answer.
I changed it, and added called the second method where you put it, but when compiled it said: java.lang.ArrayIndexOutOfBoundsException: 10000
I've played areound my code and it compiles. however when you look at the file it printed to, random letters ([D@76f1fad1[D@889ec59...) are seen. Why is this happening? (I've placed my current code above)
That is the string representation of an array of double. To get a readable representation you should call Arrays.toString like you had in the original code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.