0

我从一个看起来像这样的文件中读取

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("");
         }
    }    
}
4

3 回答 3

4

从逻辑上讲,您的 else 部分有一些问题。每条线代表二维数组中的一行。它应该有点如下 -

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
} 
于 2012-10-01T21:06:22.500 回答
4

arr未正确初始化:

private static int[][] arr;

改成:

private static int[][] arr = new int[5][5]; // for example. Dimensions
                                            // guessed from file content.
于 2012-10-01T20:54:47.683 回答
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 循环并从第二行而不是第一行开始。所以你的整个代码将是:

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("");
        }
    }    
}

希望能帮助到你!

于 2012-10-02T00:17:18.150 回答