Nazhirin Imran
Nazhirin Imran

Reputation: 129

How do I filter specific string in text file?

How do I create a program that will only continue if the specific data is in the text file. Is it possible to use BufferedReader/FileReader to read and search in every line until it found a match input data from user. Let say I'm creating a login form and the data from the text file would be :

username;password
username1;password1

So here I'm quite confused with the if-statement, how do I make it possible to read every line in the textfile until it found the correct match and allow the user to proceed to the next frame?

Upvotes: 1

Views: 2044

Answers (1)

Shar1er80
Shar1er80

Reputation: 9041

If I'm reading this correctly, try the following:

List<String> credentials = new ArrayList<>();
// Populate this list with all of your credentials

BufferedReader bReader = new BufferedReader(new FileReader(textFile));
boolean foundCredentials = false;

String line;
while ((line = bReader.readLine()) != null) {
    // Set your condition to analyze the line and find the credentials you are looking for
    if (credentials.contains(line)) {
        foundCredentials = true;
        break;
    }
}
bReader.close();

if (foundCredentials) {
    // Proceed to next frame
}

Upvotes: 2

Related Questions