我正在尝试创建一个程序,从文本文件中读取信息并将其发送到2个矩阵表。我该如何解决?

时间:2015-05-02 23:30:17

标签: arrays

The file contains:
matrix 
row
-1
2
-2
0
row
-3
4
7
2
row
6
0
3
1
matrix
row
-1
3
row
0
9
row
1
-11
row
4
-5 

此信息需要发送到矩阵表或2D数组。 这是发送给文件阅读器的信息。 这是我到目前为止的代码:

public class MatrixTester {

public static int[][] mResult;

/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */
public static void main(String[] args) throws IOException 
{
    String fileName;
    fileName = "C:\\Users\\Adam\\Documents\\NetBeansProjects\\MatrixTester\\MatrixData.txt";


    int a[][] = new int[3][4];
    int b[][] = new int[4][2];


    String line = " ";

    FileReader fr = new FileReader(fileName);

    BufferedReader bf = new BufferedReader(fr);

    int lineCount = 0;

    String[] numbers = (bf.readLine()).split("\n");


        while ((line = bf.readLine()) != null)
        {
            numbers = line.split(" ");

                    for(int i = 0; i < numbers.length; i++)  
                        {
                            // Get line values
                            String[] values = numbers[i].split("\n");

                            String delims = "0123456789";
                            StringTokenizer integers = new StringTokenizer(values[i], delims);

                            while(integers.hasMoreTokens())
                            {
                                integers.nextToken();

                                for(int j = 0; j < values.length; j++) 
                                    {

                                    for(int k = 0; k < lineCount; k++)
                                        {
                                            // Punt in Matrix
                                            a[i][j] = Integer.parseInt(values[j]);
                                            lineCount++;
                                        }
                                    }
                            }
            }



            /*
            */
        }

        bf.close();


    //Outputting information.
    System.out.println(Arrays.deepToString(a) + "\t");
}   
}

我到目前为止的代码应该将信息发送到第一个2D数组,我还没有到第二个数组。到目前为止,我得到的代码输出是[[0,0,0,0],[0,0,0,0],[0,0,0,0]]。 我该如何解决这个问题?

0 个答案:

没有答案