2011-04-29 68 views
0

可能重复:
How to print 2D Array from .txt file in Java如何在java中打印2d数组中的文本文件?

8.00 28.00
18.00 28.00 8.00 23.00
12.00 20.00 15.00 30.00 12.00 32.00 12.00 20.00 18.00 31.00 29.00 25.00 6.00 28.00
7.00 28.00
6.00 24.00 14.00 30.00 11.00 23.00 12.00 20.00 31.00 24.00 11.00 20.00 17.00 23.00 14.00 32.00 15.00 23.00 8.00 20.00
17.00 31.00 7.00 20.00 12.00 23.00 15.00 20.00 12.00 20.00 21.00 20.00 27.00 27.00 18.00 20.00 25.00 27.00 46.00 13.00 26.00 10.00 47.00 22.00 44.00 14.00 34.00 4.00 34.00 4.00
44.00 7.00
39.00 5.00
20.00 0.00
43.00 11.00 43.00 25.00 34.00 2.00
25.00 10.00 50.00 9.00
25.00 9.00
39.00 2.00
34.00 7.00
44.00 15.00 36.00 3.00
30.00 5.00
49.00 21.00 42.00 7.00
35.00 1.00
30.00 2.00
31.00 13.00 53.00 12.00 30.00 4.00
26.00 4.00
50.00 55.00 57.00 51.00 62.00 52.00 56.00 52.00 59.00 40.00 61.00 68.00 66.00 49.00 57.00 49.00 62.00 58.00 47.00 58.00 53.00 30.00 60.00 54.00 55.00 48.00 56.00 65.00 67.00 56.00 55.00 43.00 52.00 49.00 67.00 62.00 68.00 61.00 65.00 58.00 46.00 53.00 46.00 49.00 47.00 30.00 64.00 22.00 64.00 54.00 63.00 64.00 63.00 56.00 64.00 44.00 63.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 输出请帮我打印数组形式的文件

+0

可能与以下内容有关:http://stackoverflow.com/questions/5831001/to-print-the-text-file-in-2d-matrix-of-same-dimension-given-in-file-using-java – 2011-04-29 11:48:12

+0

可能涉及:http://stackoverflow.com/questions/5830786/i-want-to-print-text-file-into-2d-array-of-same-dimensions-given-in-text-file – 2011-04-29 11:48:32

+0

@ Andreas_D这似乎都来自同一所学校:) – 2011-04-29 11:48:47

回答

2

你总是打印System.out.print(" "+data[86][1]);和你阅读之前初始化排87。

你也做double [][] data = new double [87][2];这似乎是[行] [col],但你可以使用[col] [row]设置值。请修复该命令以保持一致。你不会在该行上得到ArrayIndexOutOfBoundsException s:
data[col][row] = Double.parseDouble(st.nextToken());? (编辑:我刚刚看到你的其他问题,所以你确实得到了AIOOBE;))

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

编辑:我现在停在这里。我建议你在调试器中抛出你的程序,看看你的代码真的在做什么。

0

您应该学习如何使用调试器! Here是一个好开始!

相关问题