我正在嘗試從文本中讀取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
非常感謝你的回覆,但我寫它與同樣的錯誤 –
您可以發佈更新後的代碼,請與給還更新錯誤?因爲我猜這個錯誤至少已經改變了。 –