jeudi 5 janvier 2017

Why doesn't .equals() compare these strings?

I have txt file called samplename, in that file there are 3 lines values:

 samplename
 samplepassword
 %%%%%%%%%%%%%%%%%

The Programm is about a login-register system. When I type

 samplename

in the first JTextField and

  samplepassword +

in the second JTextField and press the login button, it supposed to check if I have such account, if it has the right username&password. The Programm reads the things in, just it cant compare it to the the JTextFields. What am i doing Wrong?!?:

private static String check = null;
private static String check2 = null;




loginButton.addActionListener(new ActionListener(){

        @Override
        public  void actionPerformed(ActionEvent e) {
            Scanner scanner = new Scanner(System.in);
            BufferedReader rd = null;
            String line;
            boolean gothrew = false;
            boolean ifDOES = false;
            String Username;
            String Password;
            Password = login_username.getText().trim();
            Username = login_password.getText().trim();
            try{
            rd = new BufferedReader(new FileReader("C:\\users\\Dominik Gyarmati\\AppData\\Roaming\\KeepSafe\\users\\" +login_username.getText() +".txt" ));
            }
            catch (FileNotFoundException ex){
                JOptionPane.showMessageDialog(null, "Your Account doesn't exist");

            }
            try{
                while ((line = rd.readLine()) != null){

                    if(check == null){
                    check = line;
                    check.trim();
                    gothrew = true;
                    System.out.println(check);


                    }
                    else if(gothrew == true){
                    check2 = line;
                    check2.trim();
                    System.out.println(check2);
                    gothrew =false;
                    ifDOES = true;
                    }


                    else if (check.equals(Username) && check2.equals(Password)) {

                    JOptionPane.showMessageDialog(null, "You've logged into your account");
                    check = null;
                    check2 = null;
                    ifDOES = false;
                    }
                    else
                    { 
                    JOptionPane.showMessageDialog(null, "Username or Password is wrong");
                    check = null;
                    check2 = null;
                    ifDOES = false;
                        }

                }
            }
            catch(IOException ioexex){
            }

        }



        });

Thanks for the Help!

Aucun commentaire:

Enregistrer un commentaire