“线程中的异常”;“主要”;当读取文本文件时,java.util.NoSuchElementException扫描仪
我试图在我的程序中实现扫描器在文本文件中搜索,直到找到一行与其搜索的字符串相同的内容。 我不断得到以下错误:“线程中的异常”;“主要”;当读取文本文件时,java.util.NoSuchElementException扫描仪,java,error-handling,exception-handling,java.util.scanner,Java,Error Handling,Exception Handling,Java.util.scanner,我试图在我的程序中实现扫描器在文本文件中搜索,直到找到一行与其搜索的字符串相同的内容。 我不断得到以下错误: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1371) at hangman.HangmanArr.<init
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at hangman.HangmanArr.<init>(HangmanArr.java:62)
at hangman.HangmanApp.main(HangmanApp.java:18)
课程的完整代码是:
package hangman;
import java.io.*;
import javax.swing.*;
import java.util.*;
/**
*
* @author ghost
*/
public class HangmanArr {
String letter;
int x = 0;
String word;
String dashWord;
String newWord;
String username;
String password;
private Hangman[] arrUsers = new Hangman[100];
public HangmanArr() {
JOptionPane.showInputDialog(null, "The aim of Hangman is to form "
+ "a word \nby guessing individual letters \nof the word "
+ "before a"
+ " hanging man \nand gallows are built. Every letter\n "
+ "that is entered "
+ "which does not \nappear in the word will contribute to "
+ "\nthe hanging"
+ " man and gallows; by adding \na single component to "
+ "drawing – \nif the hanging "
+ "man and gallows are \ncomplete before guessing the "
+ "complete word;\n you have lost "
+ "the game. Goodluck!" + "\nPress Enter to continue", "H_NGM_N", JOptionPane.INFORMATION_MESSAGE);
String ans = " ";
ans = JOptionPane.showInputDialog(null, "Please enter an option of "
+ "your choice\n"
+ "A – login\n"
+ "B - Sign up\n"
+ "C - Scoreboard\n"
+ "D - quit", "Menu A", JOptionPane.QUESTION_MESSAGE).toUpperCase();
if (ans.equals("A")) {
username = JOptionPane.showInputDialog(null, "Enter usesrname:", "Login 1/2", JOptionPane.QUESTION_MESSAGE);
try {
Scanner scFile = new Scanner(new File("TextFileB.txt"));
String line = scFile.nextLine();
int flse = 0;
String user = " ";
while (scFile.hasNext() || flse == 0) {
line = scFile.nextLine();
Scanner scLine = new Scanner(line).useDelimiter("#");
user = scFile.next();
if (user.equals(username)) {
password = JOptionPane.showInputDialog(null, "Welcome " + username + ". \n Please enter your password to play", "Login 2/2", JOptionPane.QUESTION_MESSAGE);
flse++;
}
}
scFile.close();
} catch (IOException i) {
System.out.println("Text File could not be found");
}
}
if (ans.equals("B")) {
username = JOptionPane.showInputDialog(null, "Enter usesrname:", "Sign Up 1/2", JOptionPane.QUESTION_MESSAGE);
password = JOptionPane.showInputDialog(null, "Welcome " + username + ". \n Please enter your password to play", "Sign Up 2/2", JOptionPane.QUESTION_MESSAGE);
File add = new File("TextFileB.txt");
try {
PrintWriter fw = new PrintWriter(new FileWriter(add, true));
fw.write(username + "#" + password + "#");
fw.println();
fw.close();
} catch (IOException e) {
System.out.println("Could not locate text file to store data");
}
}
}
}
您的
while
状况有问题
如果到达文件末尾时没有找到用户名,flse
的值仍然是0
,因此循环条件将被评估为true
,这意味着您将到达scFile.nextLine()
调用,该调用将导致一个NoTouchElementException
,因为您已到达文件末尾
您必须将该条件更改为:
scFile.hasNext() && flse == 0
你是说
user=scFile.next()
将成为user=scLine.next()
?现在我不知道你在哪里使用它,如果你调用scFile.next
两次,每次都不检查,你可能会遇到I/O错误。要检查的代码是什么?这是我修改过的代码:你能替换user=scLine.next()
看看它是否有效吗?
scFile.hasNext() && flse == 0