Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
need help!. how do you display the name of the person who just logged in from .txt file?, here is my login code

Java
<pre>public void Masuk(){
    try {
    String lokasi = "D:/settings.txt";
            String username = txtUser.getText();
            String password = txtPass.getText();
    
    
            FileReader fr = new FileReader(lokasi);
            BufferedReader br = new BufferedReader(fr);
            String line, user, pass;
            boolean isLoginSuccess = false;
           while ((line = br.readLine()) != null) {
                user = line.split(" ")[1].toLowerCase();
                pass = line.split(" ")[2].toLowerCase();
                if (user.equals(username) && pass.equals(password)) {
                    isLoginSuccess = true;
                    this.dispose();
                    new Main_Menu(this, rootPaneCheckingEnabled).show();
                    
                    break;
                } 
           }
                
                
              if (!isLoginSuccess) {
                JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD WRONG", "WARNING!!", JOptionPane.WARNING_MESSAGE);
            }
            fr.close();
         
            
    }catch(Exception e){
    e.printStackTrace();
        }
            }


and here is my jdialog form to display the name of the person who just logged in

Java
public void Berhasil(){
        String data = "D:/Settings.txt";
        
        
        
       try {
            FileReader fr = new FileReader(data);
           BufferedReader br = new BufferedReader(fr);
           String line = br.readLine(),nama;
           
           nama = line.split(" ")[0].toLowerCase();
         String message = "Selamat datang "+ nama;
           
            
            
            String text;
            
            while ((line = br.readLine()) != null)
                
            txtBerhasil.setText(""+message);
        } 
        catch (FileNotFoundException fnfe) {
            fnfe.getMessage();
        } 
        catch (IOException ioe) {
            ioe.getMessage();
        }
                
                
        
    }


and this is how my .txt file look like

Luthfi Luthfi Hehe
Luthfi Fitra Hehe

i supposed to display the first word only, because the first word is the name, and the second word is the username, and the third word is the password

What I have tried:

i already tried questioning at the stackoverflow but i dont get any big help

ps: sorry for my bad english
Posted
Comments
Mohibur Rashid 6-May-18 0:59am    
what's in `line.split(" ")[0].toLowerCase()`
?
Richard MacCutchan 6-May-18 4:03am    
You should read the file once only. As you read each line you can split it into three variables: Name, Username, and Password. Then after comparing the values, if you get a match you can display the name that you already have saved. And don't forget in a real world application, storing passwords in clear text is very dangerous.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900