2017-09-27 52 views
-2

我試圖在我的程序中實現掃描程序搜索文本文件,直到找到與它正在搜索的字符串相同的行。 我不斷收到以下錯誤:線程「主」java.util.NoSuchElementException掃描程序中的異常通過文本文件讀取

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"); 
      } 
     } 
    } 
} 
+1

你的意思是'用戶= scFile.next();'是'用戶= scLine.next()'?現在我看不到你在哪裏使用它,如果你每次調用'scFile.next'兩次都沒有檢查你可能會遇到I/O錯誤。 –

+0

要檢查的代碼是什麼?這是我改變的代碼: –

+0

你能替換'user = scLine.next()'並且看它是否工作嗎? –

回答

0

您的while條件的問題。

如果到達文件的末尾沒有找到用戶名,的flse值仍然會0,使循環條件進行評估true,這意味着你將達到scFile.nextLine()調用,這將導致一個NoSuchElementException因爲你已經到達文件的末尾。

您需要更改條件是:

scFile.hasNext() && flse == 0 
相關問題