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 部分に問題があります。各行は、2 次元配列の行を表します。それは次のようになるはずです -

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 ループに入り、最初の行ではなく 2 番目の行から開始します。したがって、コード全体は次のようになります。

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 に答える