내 프로그램에서 검색하는 문자열과 동일한 라인을 찾을 때까지 스캐너가 텍스트 파일을 검색하도록 구현하려고합니다. 나는 다음과 같은 오류가 계속 :스레드 "main"의 예외 java.util.NoSuchElementException 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>(HangmanArr.java:62)
at hangman.HangmanApp.main(HangmanApp.java:18)
스캐너에 대한 코드는 다음과 같습니다
는경우 (ans.equals는 ("A")) { 이름 = JOptionPane.showInputDialog (NULL, "여기서 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");
}
}
가와 클래스의 전체 코드는 다음과 같습니다
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");
}
}
}
}
당신은'사용자 = scFile.next을 (의미합니까;''사용자 = scLine.next()'이 될? 지금 당장 나는 그것을 사용하는 곳과 I/O 오류가 발생할 때마다'scFile.next'를 두 번 호출하지 않고 호출한다고 보지 않는다. –
확인할 코드는 무엇입니까? 이것은 나의 변경된 코드입니다 : –
'user = scLine.next()'를 대체하고 그것이 작동하는지 볼 수 있습니까? –