【发布时间】:2016-02-23 18:49:09
【问题描述】:
我有多行输入,其中每一行都是矩阵行。我将此行保存为字符串,然后我想根据空格拆分此字符串,但由于更好的可读性,未定义数字之间的空格数。因此,当我想在之后解析为 int 时,它会引发错误,因为在某些地方有多个空格。有什么解决方案可以解决这个问题吗?谢谢
这是我的代码,我是如何解决的
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
int[][] matrix=new int[n][n];
String[] temp;
for (int row = 0; row < n; row++) {
line =br.readLine();
temp = line.split("\\s+");
for(int i=0;i<n;i++){
matrix[i][row]=Integer.parseInt(temp[i]);
}
这里是示例输入
10 10 0 10 5
5 20 10 7 12
1 2 3 5 9
10 15 20 35 2
2 15 5 15 2
【问题讨论】:
-
你能提供你得到的错误信息吗?有了这个,我们可以更轻松地为您提供帮助。
-
你的
line.split("\\s+");应该已经成功了。你看到了什么错误?