1

Banker's Algorithm を Java で実装しようとしていますが、配列の読み込みに問題があります。ここに私が取り組んでいるコードがあります

public static void main(String[] args) throws FileNotFoundException {
    String filename = null;
    int need[][];
    int allocate[][];
    int max[][];
    int available[][];
    int n = 0;
    int m = 0;
    int lineCount = 0;
    Scanner in = new Scanner(System.in);

    System.out.println("Enter the file name.");
    filename = in.nextLine();

    File textFile = new File(filename);
    Scanner input = new Scanner(textFile);

    max = new int[n][m];
    allocate = new int[n][m];
    need = new int[n][m];
    available = new int[1][m];

    n = input.nextInt();
    m = input.nextInt();
    System.out.print("Number of Processes: " + n);
    System.out.print("\nNumber of Processes: " + m);

    max = new int[n][m];
    allocate = new int[n][m];
    need = new int[n][m];
    available = new int[1][m];

    String line = input.nextLine();

    while (line != null && lineCount < n) {

        String[] temp = line.split(" ");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                allocate[i][j] = Integer.parseInt(line);
                System.out.println("here");
            }
            line = input.nextLine();
            lineCount++;
        }
    }
}

私のサンプル ファイルには、このデータが含まれています。

5

4

0 0 1 2 1 0 0 0 1 3 5 4 0 6 3 2 0 0 1 4

0 0 1 2 1 7 5 0 2 3 5 6 0 6 5 2 0 6 5 6

1 5 2 0

1:0 4 2 0

そのため、これを実行しようとしているときに、さまざまなエラーが発生しました。現在、 NumberFormatException: For input string "" エラーが発生しています。どんな助けでも大歓迎です。

4

1 に答える 1

0

You have a bunch of really small arrays, and you never increase the sizes of them:

int n = 0;
int m = 0;
...
max = new int[n][m];       // 0x0 array, unused
allocate = new int[n][m];  // 0x0 array, likely the culprit
need = new int[n][m];      // 0x0 array, unused
available = new int[1][m]; // 1x0 array, unused

Of these arrays, only allocate is used, and you are using it later in a for loop:

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            // OutOfBounds is probably here:
            allocate[i][j] = Integer.parseInt(line);
            System.out.println("here");
        }
        line = input.nextLine();
        lineCount++;
    }

Also, you are running Integer.parseInt(line) which is attempting to parse the whole line. You should just parse a single token at a time which would be Integer.parseInt(temp[someindex]).

于 2015-12-01T00:55:32.730 に答える