gpt4 book ai didi

java - 如何在java中打印二维数组中的文本文件?

转载 作者:行者123 更新时间:2023-12-02 08:18:56 26 4
gpt4 key购买 nike

Possible Duplicate:
How to print 2D Array from .txt file in Java

8.00 28.00
18.00 28.008.00 23.00
12.00 20.0015.00 30.0012.00 32.0012.00 20.0018.00 31.0029:00 25:006.00 28.00
7.00 28.00
6.00 24.0014.00 30.0011.00 23.0012.00 20.0031.00 24.0011.00 20.0017.00 23.0014.00 32.0015.00 23.008.00 20.00
17.00 31.007.00 20.0012.00 23.0015:00 20:0012.00 20.0021.00 20.0027.00 27.0018:00 20:0025.00 27.0046.00 13.0026:00 10:0047.00 22.0044.00 14.0034.00 4.0034.00 4.00
44.00 7.00
39.00 5.00
20.00 0.00
43.00 11.0043.00 25.0034.00 2.00
25.00 10.0050.00 9.00
25.00 9.00
39.00 2.00
34.00 7.00
44.00 15.0036.00 3.00
40.00 5.00
49.00 21.0042.00 7.00
35.00 1.00
30.00 2.00
31.00 13.0053.00 12.0040.00 4.00
26.00 4.00
50.00 55.0057.00 51.0062.00 52.0056.00 52.0059.00 40.0061.00 68.0066.00 49.0057.00 49.0062.00 58.0047.00 58.0053.00 40.0060.00 54.0055.00 48.0056.00 65.0067.00 56.0055.00 43.0052.00 49.0067.00 62.0068.00 61.0065.00 58.0046.00 53.0046.00 49.0047.00 40.0064.00 22.0064.00 54.0063.00 64.0063.00 56.0064.00 44.0063.00 40.00

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;


public class csvimport5 {


public static void main(String[] args) throws IOException {


double [][] data = new double [87][2];
File file = new File("buydata.txt");
int row = 87;
int col = 2;
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line ;


//read each line of text file
while((line = bufRdr.readLine()) != null && row < data.length)
{

StringTokenizer st = new StringTokenizer(line," ");
while (st.hasMoreTokens())
{

//get next token and store it in the array
data[col][row] = Double.parseDouble(st.nextToken());
col++;

}
col=0;

}

row++;

for( row = 0; row<data.length; row++){
for( col = 0; col<data.length; col++){

System.out.print(" "+data[86][1]);
}
System.out.println();
}






}

}

它给出的输出类似于 0.0 0.0 0.0 0.0请帮我以数组形式打印该文件

最佳答案

你总是打印System.out.print(" "+data[86][1]);并且在读取之前将行初始化为 87。

你也这样做double [][] data = new double [87][2];这似乎是 [row][col],但随后您使用 [col][row] 设置值。请修正顺序以保持一致。难道你不明白ArrayIndexOutOfBoundsException在那一行:
data[col][row] = Double.parseDouble(st.nextToken()); ? (编辑:我刚刚看到你的其他问题,所以你确实得到了 AIOOBE ;))

这个 while 循环永远不会运行:while((line = bufRdr.readLine()) != null && row < data.length) 。您将 row 初始化为 87,它等于 data.length,因此语句 row < data.length总是错误的。

编辑:我现在就停在这里。我建议您将程序放入调试器中,看看您的代码到底做了什么。

关于java - 如何在java中打印二维数组中的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5831535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com