将文件读入2d Array Java

时间:2012-10-01 20:52:57

标签: java multidimensional-array

我正在阅读一个看起来像这样的文件

5

0 3 1 4 2

4 0 6 1 4

1 2 0 6 5

6 3 1 0 8

4 1 9 3 0

第一个数字无关紧要。我想从第2行开始阅读。我能够读取第1行并存储它但我无法读取数组的其余部分。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class TSM {
private static int numOfCities;
private static int[][] arr;

public static void main(String[] args) throws NumberFormatException, IOException{
    numOfCities = 0;

    BufferedReader br = new BufferedReader(new     FileReader("/Users/sly/Desktop/tsp.txt"));   
    String line = " ";
    String [] temp;

    while ((line = br.readLine())!= null){ 

        if(line.trim().length() ==1) { //set value of first line in file to be numOfCities
            numOfCities = Integer.parseInt(line);
            System.out.println("There are"+ " " + numOfCities+ " " + "Number of cities");

        } else{ 
            temp = line.split(" "); //split spaces

            for(int i = 0; i<arr.length; i++) {
                for (int j = 0; j<arr.length; j++) {    
                    arr[i][j] = Integer.parseInt(temp[i]);

                }
            }
        }
    }
    printArray();
}

public static void printArray () { 
         for (int i =0; i <arr.length; i++) {
          for (int j = 0; j < arr.length; j++) {
            System.out.print(arr[i][j]);
          }
          System.out.println("");
         }
    }    
}

3 个答案:

答案 0 :(得分:4)

arr尚未正确初始化:

private static int[][] arr;

更改为:

private static int[][] arr = new int[5][5]; // for example. Dimensions
                                            // guessed from file content.

答案 1 :(得分:4)

从逻辑上讲,你的其他部分存在一些问题。每行代表两个维度数组中的一行。它应该有点如下 -

else { 
    temp = line.split(" "); //split spaces 
    int[] newRow = new int[temp.length];
    //for(int i = 0; i<arr.length; i++){ 
    for (int j = 0; j<temp.length; j++){ 
        newRow[j] = Integer.parseInt(temp[i]); 
    } 
    //} 
    //declare currentRowIndex outside while loop with initial value of 0
    arr[currentRowIndex] = newRow; 
    //increment currentRowIndex on each iteraton of the while loop
} 

答案 2 :(得分:1)

你可以在循环中取出这部分代码:

if(line.trim().length() ==1) { //set value of first line in file to be numOfCities
    numOfCities = Integer.parseInt(line);
    System.out.println("There are"+ " " + numOfCities+ " " + "Number of cities");
} else {

因为你可以做的就是在while循环之上放这个:

numOfCities = Integer.parseInt(br.readLine());
System.out.println("There are"+ " " + numOfCities+ " " + "Number of cities");

,它将做的是读取第一行,将其设置为城市数量,然后进入while循环并从SECOND行而不是第一行开始。所以你的整个代码将是:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class TSM {
    private static int numOfCities;
    private static int[][] arr = new int[5][5];

    public static void main(String[] args) throws NumberFormatException, IOException{
        numOfCities = 0;

        BufferedReader br = new BufferedReader(new FileReader("/Users/sly/Desktop/tsp.txt"));   
        String line = " ";
        String [] temp;

        numOfCities = Integer.parseInt(br.readLine()); //Reads first line then starts at while loop in second
        System.out.println("There are"+ " " + numOfCities+ " " + "Number of cities");

        while ((line = br.readLine())!= null){ 
            temp = line.split(" "); //split spaces

            for(int i = 0; i<arr.length; i++) {
                for (int j = 0; j<arr.length; j++) {    
                    arr[i][j] = Integer.parseInt(temp[i]);
                }
            }

        }
        printArray();
    }

    public static void printArray () { 
        for (int i =0; i <arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[i][j]);
            }
            System.out.println("");
        }
    }    
}

希望它有所帮助!