Java扫描程序抛出NoTouchElementException:未找到行
我不知道为什么它不起作用。我反复检查了FileWriter中的文件是否有文本(由另一个PrintWriter在前面的单独程序中输入),但while语句似乎没有运行。注释出来的行是我正在运行的各种测试,试图找出发生了什么。我试图解决的是让它遍历数组,并为所有人添加一个组ID。如果有人知道发生了什么,我们将不胜感激。我会为任何格式错误提前道歉,任何关于如何更容易获得帮助的评论也将不胜感激Java扫描程序抛出NoTouchElementException:未找到行,java,while-loop,java.util.scanner,printwriter,Java,While Loop,Java.util.scanner,Printwriter,我不知道为什么它不起作用。我反复检查了FileWriter中的文件是否有文本(由另一个PrintWriter在前面的单独程序中输入),但while语句似乎没有运行。注释出来的行是我正在运行的各种测试,试图找出发生了什么。我试图解决的是让它遍历数组,并为所有人添加一个组ID。如果有人知道发生了什么,我们将不胜感激。我会为任何格式错误提前道歉,任何关于如何更容易获得帮助的评论也将不胜感激 public static void updateWinners(Person[] Players, int n
public static void updateWinners(Person[] Players, int n)
throws FileNotFoundException {
// n is 2 or 4 depending on round
File fileS = new File(
"C:\\Users\\Patrick\\Desktop\\New folder\\FileWriter\\Win");
File fileP = new File(
"C:\\Users\\Patrick\\Desktop\\New folder\\Bracket\\Win");
Scanner fs = new Scanner(fileS);
PrintWriter writer = new PrintWriter(fileP);
//int q=0;
while (fs.hasNextLine()) {
//System.out.println(Players[q].toString());
for (int i = 0; i < Players.length; i++) {
if (fs.nextLine().equals(Players[i].toString())) {
Players[i].addGroup(alpha[i / n]);
System.out.println(Players[i].toString());
writer.println(Players[i].toString());
}
}
//q++;
}
writer.close();
fs.close();
}
publicstaticvoidupdatewinner(Person[]玩家,int n)
抛出FileNotFoundException{
//n是2或4,取决于轮数
文件=新文件(
“C:\\Users\\Patrick\\Desktop\\New folder\\FileWriter\\Win”);
File fileP=新文件(
“C:\\Users\\Patrick\\Desktop\\newfolder\\Bracket\\Win”);
扫描仪fs=新扫描仪(文件);
PrintWriter=新的PrintWriter(文件);
//int q=0;
while(fs.hasNextLine()){
//System.out.println(Players[q].toString());
for(int i=0;i
像这样做
while (fs.hasNextLine()) {
String s = fs.nextLine();
//System.out.println(Players[q].toString());
for (int i = 0; i < Players.length; i++) {
if (s.equals(Players[i].toString())) {
Players[i].addGroup(alpha[i / n]);
System.out.println(Players[i].toString());
writer.println(Players[i].toString());
}
}
//q++;
}
while(fs.hasNextLine()){
字符串s=fs.nextLine();
//System.out.println(Players[q].toString());
for(int i=0;i
说明:
每次在players循环中调用fs.nextLine()
时。它从文件中读取新行。因此,在完成所有播放机之前,将读取文件的所有行
当您尝试读取并且文件中没有更多数据时,扫描仪会引发异常
抛出:
无接触元素异常-如果未找到行
if(fs.nextLine().equals(Players[i].toString())
这就是问题所在。请尝试在while循环之前打印文件.exists()。所有这些反斜杠使路径格式看起来非常不可靠。由于您在for循环中使用.nextLine(),因此当while循环开始时,您可能会有一个新行,但随后在循环中用完。