我正在尝试从文本中读取adjMatrix图形,它只读取第一行,但是我遇到了这个错误。有什么建议吗?我怎样才能从inp文本中读取adjMatrix图形
java.lang.NumberFormatException: For input string: "0 1 1 1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
public static void main(String[] args)throws IOException {
int row_num = 0;
//InputGraph gr = new InputGraph("input.txt");
// Cells w= gr.copyMatrix();
// w.printAdjMatrix();
try {
String fileName = ("input.txt");
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
//get number of vertices from first line
String line = br.readLine();
System.out.println("hi");
//InputGraph.size = Integer.parseInt(line.trim());
InputGraph.size=Integer.parseInt(line);
InputGraph.adMatrix = new int[InputGraph.size][InputGraph.size];
br.readLine();
for (int i = 0; i < InputGraph.size; i++) {
line = br.readLine();
String[] strArry = line.split(" ");
for (int j = 0; j < InputGraph.size; j++) {
InputGraph.adMatrix[i][j] = Integer.parseInt(strArry[j]);
if (Integer.parseInt(strArry[j]) == 1)
row_num++;
}
}
} catch (Exception e4) {
e4.printStackTrace();
}
输入文本文件
0 1 1 1
0 0 0 0
1 1 0 1
0 1 0 0
非常感谢你的回复,但我写它与同样的错误 –
您可以发布更新后的代码,请与给还更新错误?因为我猜这个错误至少已经改变了。 –