Results 1 to 2 of 2
  1. #1
    maddiexc is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Help with calling methods properly in java code

    My program compiles and is supposed to write the coordinates generated from the program to a separate file. When i run the program, all that shows up in the file it writes to is:

    Java Code:
    [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]
    I think this may be because I did not properly call the method writefile directly in the main method and because of this those 0's come up:

    Java Code:
    fileOut.println(java.util.Arrays.deepToString(locations) + ", ");
    How can I call the writefile method properly so that the actual coordinates will print to the file instead of [0.0, 0.0]

    Here's the full code:
    Java Code:
    import java.io.*;
    
    public class J4
    {
      public static void main (String [] args) throws IOException
      {
        int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100
        
        //arrays are initializewd and declared
        double [] lengthscale = new double [dimension];
        double [][] locations = new double [numpoints][dimension];
        
        PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));
        
        for(int m=0; m <length; m++){//for loop
          fileOut.println(java.util.Arrays.deepToString(locations) + ", ");//writes to file
        }
        fileOut.close ();//close file
        
      }//end main
      
      public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
      {
        
        for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
          lengthscale[a] = length;//stores array
        }//end for loop
        
        for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
          for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
            locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within 
              return locations[x][y];
    
          }//end nested for loop
        }//end for loop
        
        //if program doesnt run through loop.. alternative return statement (but
         double b= 1;    
         return b;
      }//end writefile methos
    }//end cass

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: Help with calling methods properly in java code

    You and incxx must be in the same class. :) Check out Creating different methods with Arrays. If you still need help, you can ask.

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

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. output giving null, methods not reading properly
    By csuever in forum New To Java
    Replies: 22
    Last Post: 04-01-2011, 02:13 AM
  3. Calling Java from C++ code
    By franklyn@bellsouth.net in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 10:06 AM
  4. calling c code from java
    By sara12345 in forum New To Java
    Replies: 14
    Last Post: 12-27-2009, 06:12 PM
  5. Calling Java methods form Python
    By mew in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 02:30 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
  •