Reputation: 101
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
?
Upvotes: 2
Views: 95
Reputation: 15310
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();
Upvotes: 2