gpt4 book ai didi

java - 从文件中读取以逗号分隔的整数到二维数组中

转载 作者:行者123 更新时间:2023-12-02 01:42:06 34 4
gpt4 key购买 nike

我的代码应该将文件中以逗号分隔的整数值读入二维数组。由于逗号,我遇到了麻烦。 我无法从原始文本文件中删除逗号。这是原始文本文件中的内容。

1, 2, 3, 4, 5
6, 7, 8, 9, 0
6, 7, 1, 2, 5
6, 7, 8, 9, 0
5, 4, 3, 2, 1

这是我的尝试。

int[][] m = new int[5][5];
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
m[row][col] = input.nextInt();
}
}

此代码在第 4 行中给出了一个 InputMismatchException。

最佳答案

我假设“输入”是扫描仪。在从输入中读取任何内容之前,将分隔符更新为换行符或逗号:

input.useDelimiter("\\n|\\s*,\\s*");

关于java - 从文件中读取以逗号分隔的整数到二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319654/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com