gpt4 book ai didi

java - 为什么我不能返回数组的值?

转载 作者:行者123 更新时间:2023-11-29 05:54:23 25 4
gpt4 key购买 nike

我是 Java 的新手,所以可能对此有一个简单的解释,但无论如何我可能会觉得自己很愚蠢。

我正在尝试使用一种方法来读取文件,从该文件数据填充二维数组,然后返回填充的数组,以便我可以在我的主类中使用它并从那里打印出数组内容。

这是我目前得到的:

public class ScoreProcessor { 
static public readFile() throws IOException {
File filedata = new File("src/JavaApp2/Data.txt");
Scanner file = new Scanner (filedata);
int row = 0, col = 0;
String[][] scores = new String[8][5];

while (file.hasNextInt()){
Scanner readfile = new Scanner (filedata);
readfile.nextLine();
readfile.useDelimiter(",");

while (readfile.hasNext(",")){
String line = readfile.next();
scores[row][col] = line;
col++;
}
row++;
col=0;
}
return scores;
}
}

任何帮助都将不胜感激,谢谢。

最佳答案

正如 Peter Lawrey 所说,您需要像这样将 rtuerntype String[][] 添加到您的方法头中:

static public String[][] readFile() throws IOException {
...

此外,如果您事先不知道大小,则不应使用数组。在这种情况下使用列表。

关于java - 为什么我不能返回数组的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12724913/

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