【发布时间】:2019-10-10 16:44:33
【问题描述】:
我想从输入中读取一个文件,这个文件看起来像:
..........
..........
..........
..........
我想要这个输入文件,读取并放入一个二维数组。
我已经试过了:
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Give the input filename");
String fileName=scanner.nextLine();
readFieldFile(fileName);
scanner.close();
}
static Cell[][] readFieldFile(String fileName) throws IOException {
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
char[][] field=new char[4][10];
String currentLine;
for(int i=0;i<4;i++) {
while((currentLine=reader.readLine())!=null) {
field[i]=currentLine.toCharArray();
}
}
return field;
}
它给出了一个错误,但我认为这是因为我使用了Cell[][]。我需要它来返回这个Cell[][],所以我可以在我的其他函数中使用它。有人知道如何获得正确的回报吗?该字段是固定的:4 行和 10 列。
【问题讨论】:
-
你遇到了什么错误?
-
类型不匹配:无法从 char[][] 转换为 Cell[][]
-
什么是单元格对象?