Login on java based from txt file

user8775227

need help!, i have an assignment i need to make an login txt based, my program only read the first line

the txt look like this

Luthfi Luthfi Fitra
Fitra Fitra Khori
Khori Fitra Syifa

but it only read the first, so i could log in only using the top account here is my code

        public Login() {
    initComponents();
    txtPass.setText("");
}

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();
               MainMenu mm = new MainMenu();
               mm.setLocationRelativeTo(null);
               mm.setVisible(true);
               break;
           }
           else{
               JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD SALAH","WARNING!!",JOptionPane.WARNING_MESSAGE);
               break;
           }
       }
       fr.close();

}catch(Exception e){
e.printStackTrace();
    }
        }

also why is everytime i insert the correct username and id it shows me that im succses but it show me the USERNAME/PASSWORD IS WRONG too

thaveethu gce

You have problem in getting substring from file, change code like following snipet

        user = line.substring(0, 6).toLowerCase();
        pass = line.substring(7, 12).toLowerCase();

Update: Based on your comment , i am changing my solution

1) Change your file format like this

Luthfi Luthfi Hehe
Fitra Luthfi Khori
Syifa Khori Luthfi
Khori Syifa Luthfi

here first word in each line is user name and second word is password

2) Change your code like following

            user = line.split(" ")[1].toLowerCase();
            pass = line.split(" ")[2].toLowerCase();

3) if any username and password match, make login and break from while loop

Update again

package problems;

import java.io.BufferedReader;
import java.io.FileReader;

public class FileRd {

    public static void main(String args[]) {
        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();
                    MainMenu mm = new MainMenu();
                    mm.setLocationRelativeTo(null);
                    mm.setVisible(true);
                    break;
                } 
            }
            if (!isLoginSuccess) {
                JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD WRONG", "WARNING!!", JOptionPane.WARNING_MESSAGE);
            }
            fr.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Reading from a txt file in Java

分類Dev

Recursively Read from a .txt file Java

分類Dev

java.util.InputMismatchException error when scanning from a .txt file

分類Dev

txt file to dictionary and login implementation Python 3

分類Dev

Reading variable from txt file

分類Dev

Downloading links from a txt file

分類Dev

Search in a file for Specific sentences in a txt file in java

分類Dev

Read Credentials by txt file and try login c#?

分類Dev

From tibble to txt or excel file in R

分類Dev

awk script to read data from txt file

分類Dev

Extracting from excel (.xlsx) writing to .txt file

分類Dev

pulling a time stamp from a txt file

分類Dev

Read individual parts from HTML .txt file

分類Dev

Creating a dictionary in python from txt file

分類Dev

Reverse a String by reading from a txt file

分類Dev

Array/List from txt file in Python

分類Dev

Searching data from txt file in iOS

分類Dev

Load rectangle data from txt file with Python?

分類Dev

Particle Tracking by coordinates from txt file

分類Dev

Substitute several words in all files in a folder based on a .txt file

分類Dev

Python - Login and download specific file from website

分類Dev

how to sort txt files in php based on the second line in each txt file

分類Dev

To write the value from resultset to text file (.txt file)

分類Dev

Replacing a substring from an .txt file and writing to a new .txt file stops after 1 line IN C

分類Dev

extract information from file based on conditions

分類Dev

how can one declare a word or number from a txt file into string

分類Dev

PowerShell - Remove spaces between words from txt file

分類Dev

Inserting text from remote .txt file with known URL into element

分類Dev

How to write data to .txt file(FileWriter) from another class in javafx

Related 関連記事

  1. 1

    Reading from a txt file in Java

  2. 2

    Recursively Read from a .txt file Java

  3. 3

    java.util.InputMismatchException error when scanning from a .txt file

  4. 4

    txt file to dictionary and login implementation Python 3

  5. 5

    Reading variable from txt file

  6. 6

    Downloading links from a txt file

  7. 7

    Search in a file for Specific sentences in a txt file in java

  8. 8

    Read Credentials by txt file and try login c#?

  9. 9

    From tibble to txt or excel file in R

  10. 10

    awk script to read data from txt file

  11. 11

    Extracting from excel (.xlsx) writing to .txt file

  12. 12

    pulling a time stamp from a txt file

  13. 13

    Read individual parts from HTML .txt file

  14. 14

    Creating a dictionary in python from txt file

  15. 15

    Reverse a String by reading from a txt file

  16. 16

    Array/List from txt file in Python

  17. 17

    Searching data from txt file in iOS

  18. 18

    Load rectangle data from txt file with Python?

  19. 19

    Particle Tracking by coordinates from txt file

  20. 20

    Substitute several words in all files in a folder based on a .txt file

  21. 21

    Python - Login and download specific file from website

  22. 22

    how to sort txt files in php based on the second line in each txt file

  23. 23

    To write the value from resultset to text file (.txt file)

  24. 24

    Replacing a substring from an .txt file and writing to a new .txt file stops after 1 line IN C

  25. 25

    extract information from file based on conditions

  26. 26

    how can one declare a word or number from a txt file into string

  27. 27

    PowerShell - Remove spaces between words from txt file

  28. 28

    Inserting text from remote .txt file with known URL into element

  29. 29

    How to write data to .txt file(FileWriter) from another class in javafx

ホットタグ

アーカイブ