0

I want to get datas from text file ordered like

qwer,qwer,qwer,qwer,wqer
qwer,qwer,qwer,qwer,wqer  

But I dont need all data from text file, I just need first 2 data of each line which are username and password. Other parts are working fine, just need code for this loginB button. usernameT passwordT are textfields that have been already created.

    loginB.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
       File loginf = new File("login.txt");

            try{
                Scanner read = new Scanner(loginf);
                read.useDelimiter(",");

                while(read.nextLine()){
                    String user = read.next();
                    String pass = read.next();
                     read.next();
                if(usernameT.getText().equals(user) && passwordT.getText().equals(pass) && admin.isSelected()){
                        new Menu();
                    }

                }
                JOptionPane.showMessageDialog(null, "Incorrect username or password");
                usernameT.setText("");
                passwordT.setText("");

                read.close();
            }
            catch (FileNotFoundException qwerty){
                    JOptionPane.showMessageDialog(null, "Can't find a text file");
            }
       }
    });

1 Answer 1

1

Is this helpfull:

boolean login = false;
while(read.nextLine() !=null){
   String user = read.next();
   String pass = read.next();
   read.next();
   if(usernameT.getText().equals(user) && passwordT.getText().equals(pass) && admin.isSelected()){
      login = true;
      break;                 
   }
}
if(login)
   new Menu();
else {
   JOptionPane.showMessageDialog(null, "Incorrect username or password");
   usernameT.setText("");
   passwordT.setText("");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.