简体   繁体   中英

Comparing username and password to data from text file using java

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM