在尝试不同的方法来使用变量时出错 [英] Error when trying to use variable in different method
问题描述
在我的最后一个问题,下面的建议<一后href=\"https://stackoverflow.com/questions/35118263/using-variable-from-another-method-in-java#comment57956881_35118263\">HERE ,我遇到了与异常的问题:java.lang.ArrayIndexOutOfBoundsException
我的程序读取从大约(目前)两个粒子和它们的当前位置,速度的文件数据,和力(与一个x,y和z为所有三个属性)。这是我的文件:
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
第一个数字是的的计数的,有多少粒子有。每个粒子都有一个ID,类型,位置,速度和力量。
我所试图做的是用的ReadFile的局部变量()
在主
,并企图修复我的previous问题。
下面是我的code:
进口的java.io.File;
进口java.io.IOException异常;
进口的java.util。*;
进口java.io.FileReader;
进口java.io.FileNotFoundException;公共类粒子{
INT particleID;
INT particleType;
双particlePosition [] =新的双[3];
双particleVelocity [] =新的双[3];
双particleForce [] =新的双[3];
静态诠释计数;
静态INT电流= 0;
静态扫描仪READDATA;公共静态无效的主要(字串[] args){ INT K = 100; //在[N / M]或[公斤*米/秒^ 2]
INT M = 1; //在[公斤]
双X0 = 1; //在[M]。
双T; //在[S]
双DT; //在[S]
双oldForce1;
双oldForce2;
双CURTIME = 0;
双finTime; T = 1 /((1 /(2 *(Math.PI)))*的Math.sqrt(2 * K / M));
的System.out.println(T); DT = T / 150; ReadFile的();
粒子[] mainParticles = Particle.readfile();
的System.out.println(第一:[+ mainParticles [0] .particlePosition [0] +,+ 0 +]);
的System.out.println(二:[+ mainParticles [1] .particlePosition [0] +,+ 0 +]);}公共静态粒子[] ReadFile的(){ 粒子[] = listParticles新粒子[统计] 尝试{
READDATA =新的扫描仪(新文件(SRC / 2particle-initial.data));
}
赶上(例外五){
的System.out.println(找不到文件);
} 数= readData.nextInt(); 而(电流I计数){
listParticles [当前] =新粒子();
listParticles [电流] .particleID = readData.nextInt();
listParticles [电流] .particleType = readData.nextInt(); listParticles [电流] .particlePosition [0] = readData.nextDouble();
listParticles [电流] .particlePosition [1] = readData.nextDouble();
listParticles [电流] .particlePosition [2] = readData.nextDouble(); listParticles [电流] .particleVelocity [0] = readData.nextDouble();
listParticles [电流] .particleVelocity [1] = readData.nextDouble();
listParticles [电流] .particleVelocity [2] = readData.nextDouble(); listParticles [电流] .particleForce [0] = readData.nextDouble();
listParticles [电流] .particleForce [1] = readData.nextDouble();
listParticles [电流] .particleForce [2] = readData.nextDouble(); 当前++;
}
电流= 0; 的System.out.println(第一:[+ listParticles [0] .particlePosition [0] +,+ 0 +]);
的System.out.println(二:[+ listParticles [1] .particlePosition [0] +,+ 0 +]); readData.close();
返回listParticles;
}
}
在你的 ReadFile的()
方法,你想创建一个大小的数组计数
在计数
已经从文件中读取,因此它总是创建大小为0(类型的默认值的数组 INT
)
以下部分更改
粒子[] = listParticles新粒子[统计] 尝试{
READDATA =新的扫描仪(新文件(SRC / 2particle-initial.data));
}
赶上(例外五){
的System.out.println(找不到文件);
} 数= readData.nextInt();
到
{尝试
READDATA =新的扫描仪(新文件(SRC / 2particle-initial.data));
}
赶上(例外五){
的System.out.println(找不到文件);
} 数= readData.nextInt(); 粒子[] = listParticles新粒子[统计]
在第2 code块变量计数设置创建阵列之前,所以它创造适当大小的阵列
After following the advice on my last question HERE , I ran into the problem with an exception: java.lang.ArrayIndexOutOfBoundsException
My program reads data from a file about (currently) two particles and their current position, velocity, and force (with an x,y, and z for all three attributes). Here is my file:
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 there are. Each particle has an ID, type, and position, velocity, and force.
What I am trying to do is to use the local variable of readfile()
in main
, and attempted to fix my previous problem.
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();
Particle [] mainParticles = Particle.readfile();
System.out.println("First: [ " + mainParticles[0].particlePosition[0] + " , " + 0 + " ]");
System.out.println("Second: [ " + mainParticles[1].particlePosition[0] + " , " + 0 + " ]");
}
public static Particle[] readfile(){
Particle [] listParticles = new Particle[count];
try{
readData = new Scanner(new File("src/2particle-initial.data"));
}
catch(Exception e){
System.out.println("Could not find file");
}
count = readData.nextInt();
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();
return listParticles;
}
}
In your readFile()
method, you are trying to create an array of size count
before count
has been read from the file, so it always creates an array of size 0 (Default value of type int
)
Change below part
Particle [] listParticles = new Particle[count];
try{
readData = new Scanner(new File("src/2particle-initial.data"));
}
catch(Exception e){
System.out.println("Could not find file");
}
count = readData.nextInt();
to
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];
In the 2nd code block variable count is set before creating array, and so it creates array of appropriate size
这篇关于在尝试不同的方法来使用变量时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!