0
我有此格式的文本文件:從文件讀入布爾矩陣在Java中
5
true, false, true, false, false
true, false, true, true, true
false, false, false, false, true
true, true, false, true, true
false, false, false, true, false
,我想讀給一個矩陣。 這裏就是我已經設法到目前爲止做:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class tests {
public static void main(String[] args) throws IOException {
System.out.println("File Name: ");
Scanner reader = new Scanner(System.in);
String fileName = reader.nextLine();
Scanner file = new Scanner(new BufferedReader(new FileReader(fileName)));
int size = file.nextInt();
System.out.println("Size: " + size);
boolean[][] matrix = new boolean[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = file.nextBoolean();
}
}
reader.close();
file.close();
}
}
但是這給了我一個錯誤:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextBoolean(Scanner.java:1782)
at tests.tests.main(tests.java:27)
如何解決這個問題的任何想法?
它仍然給我同樣的錯誤 – laker001 2015-02-06 13:08:24
@ laker001我做了一個小小的編輯。你必須添加2個'nextLine()'調用。 – Eran 2015-02-06 13:08:57