2

I want to calculate the position of two particles that will be constantly reacting between each other. My problem is that I need to bring my particle listParticle array from my readData() to my main(String[] args) method.

I have tried making the array of particles a global variable, but it always forces me to make it static and then my code does not work.

My program reads from a file, with the data as such:

2 

1 1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

2 1 1.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

The first number is count, how many particles are in the file. Each particle has an ID, type, and x, y, and z for position, velocity, and force.

Here is my code:

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class Particle 
{
    int particleID;
    int particleType;
    double particlePosition[] = new double[3];
    double particleVelocity[] = new double[3];
    double particleForce[] = new double[3];
    static int count;
    static int current = 0;
    static Scanner readData;

    public static void main(String[] args) 
    {
        int k = 100; // in [N/m] or [kg*m/s^2]
        int m = 1; // in [kg]
        double x0 = 1; // in [m]
        double t;  // in [s]
        double dt;  //  in [s]
        double oldForce1;
        double oldForce2;
        double curTime = 0;
        double finTime;

        t = 1/((1/(2*(Math.PI))) * Math.sqrt(2*k/m));
        System.out.println(t);

        dt = t/150;

        readfile();

        //System.out.println("First:  [ " + listParticles[0].particlePosition[0] + " , " + 0 + " ]"); <-- does not work
        //System.out.println("Second:  [ " + listParticles[1].particlePosition[0] + " , " + 0 + " ]"); <-- does not work
    }

    public static void readfile()
    {
        try
        {
            readData = new Scanner(new File("src/2particle-initial.data"));
        }
        catch(Exception e)
        {
            System.out.println("Could not find file");
        }
        count = readData.nextInt();

        Particle [] listParticles = new Particle[count];

        while (current < count)
        {
            listParticles[current] = new Particle();
            listParticles[current].particleID = readData.nextInt();
            listParticles[current].particleType = readData.nextInt();

            listParticles[current].particlePosition[0] = readData.nextDouble();
            listParticles[current].particlePosition[1] = readData.nextDouble();
            listParticles[current].particlePosition[2] = readData.nextDouble();

            listParticles[current].particleVelocity[0] = readData.nextDouble();
            listParticles[current].particleVelocity[1] = readData.nextDouble();
            listParticles[current].particleVelocity[2] = readData.nextDouble();

            listParticles[current].particleForce[0] = readData.nextDouble();
            listParticles[current].particleForce[1] = readData.nextDouble();
            listParticles[current].particleForce[2] = readData.nextDouble();

            current++;
        }
        current = 0;

        System.out.println("First:  [ " + listParticles[0].particlePosition[0] + " , " + 0 + " ]");
        System.out.println("Second:  [ " + listParticles[1].particlePosition[0] + " , " + 0 + " ]");

        readData.close();
    }
}

How can I send my local readData() listParticle array to my main?

3
  • Each particle has an ID, type, and x, y, and z for position, velocity, and force. That is seven variables, but I see 11 variables in your file? Commented Jan 31, 2016 at 19:23
  • So declare listParticles as a data member of the class, then expose a getter method. Commented Jan 31, 2016 at 19:27
  • @Riley Carney the x y z are for each position velocity and force, not just position Commented Jan 31, 2016 at 20:51

1 Answer 1

2

You can define your method to return the listParticle array. Like so:

Change its signature to:

public static Particle[] readfile()

And add a return statement after the close:

return listParticles;

In your main you can call this function and assign its return to a variable:

myParticleArray = Particle.readfile();
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't work for me when I change the signature of the method, I get an exception: java.lang.ArrayIndexOutOfBoundsException: 0 I added the return and the main call.
This is a whole other error you should handle... Maybe consider posting a new question with that complete error and stack trace. Send a link to it and I'll try and help
Thank you for helping out, here is my new question stackoverflow.com/questions/35120747/…

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.