经过对这个和其他许多互联网社区的全面调查后,我未能解决我的问题。这是关于ControlP5按钮,我的目标是从以前格式化的文本文件导入二维数组,并且有两列19行和空格分隔的值。现在我的代码可以工作,但是我指定用来保存txt值的2D arrray不会获得所有值,而只是第一行中的最后一对值。我知道遍历所有值的循环,但只将它们存储在第一行。我不知道如何将它推到另一排阅读。这是我的代码:通过按钮和.txt文件输入二维数组处理Java
if(theEvent.isController())
{
if(theEvent.controller().name() == "mean shape males")
{
String loadPath1 = selectInput();
reader = createReader(loadPath1); //new BufferedReader
int x=0; //rows
int y=0; //columns
String smaLine;
try
{
while ((smaLine = reader.readLine()) != null)
{
String[] values = smaLine.split(", ");
for(int i = 0; i < values.length; i++)
{
float[] testC = float(split(values[i], " "));
for (int j = 0; j < testC.length; j++)
{
mat1[j][i] = testC[j]; //THIS IS THE PROBLEMATIC MATRIX
}
}
x = x+1;
}
}
catch (IOException e)
{
e.printStackTrace();
}
mat1max = maxRet(mat1);
mat1min = minRet(mat1);
inputMat = new Grid(2,19,10,130,22,mat1,mat1min,mat1max);
}
}
我用所有的意见,我能找到的堆栈溢出主要是从这个职位How to print 2D Array from .txt file in Java但我不能老是似乎给读者移动到其他然后行第一。
您需要:a)用'println(...)'调用代码来检查变量的状态,并帮助您找出错误的位置,或者使用调试器,或者使用调试器)改进了你的代码格式,因为它非常难以阅读全部左对齐。运气。 – 2012-02-29 13:40:24
对于每一行输入,您重置'mat1 [j] [i]'的元素,写入您之前读取的数据。这是故意的吗? – Joni 2012-02-29 13:44:52
你如何初始化mat1? – Churk 2012-02-29 13:46:10