Results 1 to 9 of 9
  1. #1
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Creating different methods with Arrays

    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 where is the correct spot to place my return statement


    Java Code:
    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

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Creating different methods with Arrays

    Where do you call your writeToFile method?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Creating different methods with Arrays

    Quote Originally Posted by jim829 View Post
    Where do you call your writeToFile method?
    I've added it here, and now it only prints random letters (ex. [D@76f1fad1 [D@889ec59...) to the file. Why is this happening?

    Java Code:
    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((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

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Creating different methods with Arrays

    Because you are only printing out the first dimension of a two dimensional array. So you either
    need to print out each element or use Arrays.toString(coordinates[i]) in your print statement.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Creating different methods with Arrays

    Quote Originally Posted by jim829 View Post
    use Arrays.toString(coordinates[i]) in your print statement.
    I have changed it and when it is compiled, i receive an error saying: cannot find symbol
    symbol : variable Arrays

    Java Code:
    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

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Creating different methods with Arrays

    You need to import it. java.util.Arrays;

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Creating different methods with Arrays

    I've added it and it works. Thanks for the help!

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Creating different methods with Arrays

    Since the writeTofile method only generates random co-ordinates and does nothing about writing to a file perhaps you should rename it or rewrite your code.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

Similar Threads

  1. Arrays and Methods
    By jmscarlet9 in forum New To Java
    Replies: 3
    Last Post: 03-27-2012, 02:29 AM
  2. Question about arrays and methods
    By BenH in forum New To Java
    Replies: 3
    Last Post: 01-31-2012, 02:45 AM
  3. Using Arrays and Overloading Methods.
    By dalek in forum New To Java
    Replies: 5
    Last Post: 10-25-2011, 04:52 AM
  4. Arrays and methods
    By namie in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 09:43 AM
  5. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 07:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •