Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating methods with Arrays

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Creating 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 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


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Creating methods with Arrays

    You never ever call the writeToFile method

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Creating methods with Arrays

    I've change it and called it herre:

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

    however when I compile it it gives me the error

    writeTofile(double[],double[][],int,int,int) in program cannot be applied to (double[])

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Creating methods with Arrays

    The error means you can not call a method with the single parameter of type double[] because the method requires the 5 parameters listed in the error message. (The same 5 the the method is declared with)

  5. The Following User Says Thank You to jps For This Useful Post:

    incxx (September 23rd, 2013)

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Creating methods with Arrays

    Creating different methods with Arrays

    Duplicate post
    Improving the world one idiot at a time!

Similar Threads

  1. Methods and Arrays
    By scott201 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 11th, 2013, 10:07 PM
  2. [SOLVED] Using set/get methods and arrays
    By Shadud in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 25th, 2012, 10:11 PM
  3. Initializing and Receiving Variables, Creating Methods
    By RadiantChaos in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 12:01 PM
  4. Passing Arrays Between Methods
    By kigroy in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 10th, 2011, 10:10 PM
  5. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM

Tags for this Thread