试图从java中的文本文件中读取错误:线程“AWT-EventQueue-0”中的异常java.util.NoSuchElementException

时间:2013-08-05 18:38:54

标签: text-files awt-eventqueue

我得到一个错误,我不知道它来自哪里? 在我认为问题所在的地方:

public boolean verifyLogin(String username, String password)
{    
    //method to check if the Login details math those in the text file
    //Parameters are the username and password used to verify login
    //returns a boolean to indicate whether the login is legitimate
    boolean flag = false;
    try 
    {
        Scanner sc = new Scanner(new FileReader("src\\Users.txt"));
        while (sc.hasNext()) 
        {                     
            Scanner b = new Scanner(sc.nextLine()).useDelimiter("#");
            String uName = sc.next();
            String pWord = sc.next();

            if (username.equalsIgnoreCase(uName) & password.equalsIgnoreCase(pWord)) 
            {
                JOptionPane.showMessageDialog(null, "Login Successfull. /n Welcome");     
                flag = true;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Login Unsuccessfull. /n Please re-enter your details");
                flag = false;
            }               

        }
    } 
    catch (FileNotFoundException ex)
    {
        System.out.println("File was not found " + ex.getMessage() );
    }
    return flag;

}

这与行动方法相对应:

private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) 
{                                            
    GuiClass gC = new GuiClass();
    Welcome w = new Welcome();
    boolean f = gC.verifyLogin(UsernameField.getText(), PasswordField.getText());

    if (f = true) 
    {
        this.setVisible(false);
        w.setVisible(true);      
    }
    else
    {
        System.out.println("It works");
    }
} 

请帮我弄清楚???可能导致的错误

1 个答案:

答案 0 :(得分:0)

你可能想要通过比较运算符==

来改变你的=赋值为f
if (f == true) 
{
  this.setVisible(false);
  w.setVisible(true);      
}
else
{
  System.out.println("It works");
}