I'm trying to teach myself and I'm having trouble with this simple codes. I just want to show if the user typed the username and the password correctly, Now the problem goes like this, it displays multiple information messages if both the username and the password are incorrect or if they are both null. And also, at line 36 I'm having trouble too. Please guide me : )
Expand|Select|Wrap|Line Numbers
- String UserName = "reysean";
- String Password = "lotus18";
- boolean CheckUserName=false;
- boolean CheckPassword=false;
- //Check for username
- if (txtUserName.getText().isEmpty()){
- JOptionPane.showMessageDialog(null,"Please enter username.");
- CheckUserName=false;
- txtUserName.grabFocus();
- }
- else if (txtUserName.getText().equals(UserName)) {
- CheckUserName=true;
- }
- else{
- JOptionPane.showMessageDialog(null,"Please enter correct username.");
- CheckUserName=false;
- txtUserName.grabFocus();
- }
- //Check for password
- if (txtPassword.equals("")){
- JOptionPane.showMessageDialog(null,"Please enter password.");
- CheckPassword=false;
- txtPassword.grabFocus();
- }
- else if (txtPassword.equals(Password)) {
- CheckPassword=true;
- }
- else{
- JOptionPane.showMessageDialog(null,"Please enter correct password.");
- CheckPassword=false;
- txtPassword.grabFocus();
- }
- if ((CheckUserName=true) && (CheckPassword=true)){
- JOptionPane.showMessageDialog(null, "The username is: " + UserName + "\n The password is: " + Password );
- }