0

我收到一个错误,我不知道它来自哪里?这是我认为问题所在:

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

请帮我弄清楚错误可能是由什么引起的???

4

1 回答 1

0

您可能想要更改您的 =,它通过比较运算符 == 将 true 分配给 f

if (f == true) 
{
  this.setVisible(false);
  w.setVisible(true);      
}
else
{
  System.out.println("It works");
}
于 2013-08-05T19:24:40.073 回答