-2

I have a text file having the following design:

FirstName= xxxxx
LastName= xxxxx
Username= qwerty
Password= qwerty100
Email ID= xx@gmail.com
PhoneNumber= 12345678

____________

FirstName= yyyyy
LastName= yyyyy
Username= qwerty999
Password= pass100
Email ID= yy@gmail.com
PhoneNumber= 1235326

I have the task to code a login/signup program.The above text is generated by the signup form.The sign up program asks the user to input user name and password and if it matches any of the username with its corresponding password, the user successfully logs in with a successfully logged in message on the terminal

For example: If the user inputs username: qwerty999 and password: pass100, the program shows a successfully logged in message and if no database is found matching the user input, a error message is displayed and the program terminates

My main problem is that I cannot search for the input username and password input by the user in the text file. I want it to be searched and store in another variable which the program can compare with the user input

JvCodr
  • 7
  • 2
  • Hello JvCodr ! Since this seems to be a homework question, I wont provide you with code outright, but rather "point" you in the right direction ! You will want to read the information from the file, and then search through that information when you need to check credentials. Take a look at these resources: https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file and https://www.guru99.com/string-contains-method-java.html . If you should run into problems, just edit your question with the code of your approach so far and we will help out :) – Kujoen Dec 15 '21 at 08:01
  • This could also be usefull to solve the task: https://www.baeldung.com/string/split – Kujoen Dec 15 '21 at 08:04
  • The inclusion of the value labels in the file seems a little redundant. Wouldn't it be easier to have one record per line with the fields separated by a delimiter such as a colon? – M. Gianota Dec 15 '21 at 08:07

1 Answers1

0

After you got the file data as a string you can use Regex named group to check if this user is existing or not, for example, this code will check if it exists it will return the other info to you, and if not it will give you error message, you can extend it

public static void main(String[] args) {
    String input = "FirstName= xxxxx\n" +
            "LastName= xxxxx\n" +
            "Username= qwerty\n" +
            "Password= qwerty100\n" +
            "Email ID= xx@gmail.com\n" +
            "PhoneNumber= 12345678\n" +
            "\n" +
            "____________\n" +
            "\n" +
            "FirstName= yyyyy\n" +
            "LastName= yyyyy\n" +
            "Username= qwerty999\n" +
            "Password= pass100\n" +
            "Email ID= yy@gmail.com\n" +
            "PhoneNumber= 1235326";

    String username = "qwerty";
    String password = "qwerty100";
    Pattern pattern = Pattern.compile(
            "FirstName= ([a-zA-Z]+)\n" +
            "LastName= ([a-zA-Z]+)\n" +
            "Username= " + Pattern.quote(username) + "\n" +
            "Password= " + Pattern.quote(password) + "\n" +
            "Email ID= (.+)\n" +
            "PhoneNumber= (\\d+)");
    
    Matcher matcher = pattern.matcher(input);
    
    boolean isValidUser = matcher.find();
    if (isValidUser) {
        String firstName = matcher.group(1);
        String lastName = matcher.group(2);
        String emailId = matcher.group(3);
        String phoneNumber = matcher.group(4);
    } else {
        System.out.println("No User with this username and password");
    }
}

For more info about this topic search for Regex named group

AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30
  • I need to search for the inputs(username and password only) of the user in the text file and if it matches, store it in another variable to compare it with the user's input. I need regex for the whole file – JvCodr Dec 15 '21 at 08:29
  • You will store user input in `username` and `password` then you will start validate if it valid user now you have all this user information you can store them in User Object and user it – AmrDeveloper Dec 15 '21 at 08:31
  • Yes you should extend this example and validate user input first then user it in regex – AmrDeveloper Dec 15 '21 at 08:32
  • You can first validate username and password against regex to check if it alphanumeric only then use them – AmrDeveloper Dec 15 '21 at 08:42
  • Thanks for your suggestion and information, i have updated the answer to use Pattern quote – AmrDeveloper Dec 15 '21 at 09:16