【发布时间】:2018-07-28 09:46:35
【问题描述】:
我正在尝试读取一个包含 81 个整数的文件并用这 81 个整数填充一个数组,因此我检查了这些数字的有效性。
public static int[][] ReadIn () {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a filename: ");
int[][] grid = new int[9][9];
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
grid[i][j] = input.nextInt();
File file = new File(input);
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while((st = br.readLine()) != null)
System.out.println(st);
return grid;
}
我的问题是当我从用户那里读取文件时出现错误
CheckSudokuSolution.java:24: error: no suitable constructor found for File(Scanner)
File file = new File(input);
^
constructor File.File(String) is not applicable
(argument mismatch; Scanner cannot be converted to String)
constructor File.File(URI) is not applicable
(argument mismatch; Scanner cannot be converted to URI)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
我不确定为什么会发生此错误或如何解决它。
【问题讨论】:
-
好的,然后没有列表。您需要从
Scanner获取字符串,而不是使用Scanner本身,例如new File(input.next()). -
试试
scanner.nextLine()