1

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?

Nazhirin Imran
  • 129
  • 1
  • 11
  • 1
    As you say, iterate every line and use `String#contains`. **Note** this is really really really really really not the way to implement anything "secure" - only use for academics. – Mena Apr 23 '15 at 16:27
  • something like this to read: http://stackoverflow.com/a/326440/3419242; and something like this to do the verification: http://stackoverflow.com/a/2275035/3419242; probably this is not the best way to do it, so I will not put it as awnser – xanexpt Apr 23 '15 at 16:31
  • use `break` statement inside a `while` loop. – Alex Salauyou Apr 23 '15 at 16:31

1 Answers1

2

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
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • @NazhirinImran The code will read until it finds the credentials you want, then it stops reading and closes the file – Shar1er80 Apr 24 '15 at 14:23
  • Yes I know, but in the if statement, you write out `if (line.contains("username:password") || line.contains("username1:password1"))` So, imagine if there are like hundreds of user credential in the text file, how could I write out every single one credentials in the if statement? – Nazhirin Imran Apr 24 '15 at 14:26
  • @NazhirinImran See my changes to my answer. You can store all of your credentials in a List, then just check if the line read from the file is in your credentials List. – Shar1er80 Apr 24 '15 at 14:31