Java Valid Username Regular Expression - HackerRank Solution

Java Valid Username Regular Expression - Hacker Rank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java Valid Username Regular Expression Problem with a very easy explanation. This is the 23rd problem of Java on HackerRank. In this article, you will get more than one approach to solving this problem. So let's start-

{tocify} $title={Table of Contents}

Java Valid Username Regular Expression - Hacker Rank Solution

HackerRank Java Valid Username Regular Expression - Problem Statement

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered valid if all the following constraints are satisfied:

  • The username consists of 8 to 30 characters inclusive. If the username consists of less than 8 or greater than 30 characters, then it is an invalid username.
  • The username can only contain alphanumeric characters and underscores (_). Alphanumeric characters describe the character set consisting of lowercase characters [a-z], uppercase characters [A-Z], and digits [0-9].
  • The first character of the username must be an alphabetic character, i.e., either lowercase character [A-Z] or uppercase character [A-Z].
For example:

UsernameValidity
JuliaINVALID; Username length < 8 characters
SamanthaVALID
Samantha_21VALID
1SamanthaINVALID; Username begins with non-alphabetic character
Samantha?10_2AINVALID; '?' character not allowed

Update the value of regularExpression field in the UsernameValidator class so that the regular expression only matches with valid usernames.

Input Format

The first line of input contains an integer n, describing the total number of usernames. Each of the next n lines contains a string describing the username. The locked stub code reads the inputs and validates the username.

Constraints

  • The username consists of any printable characters.
  • 1 <= n <= 100

Output Format

For each of the usernames, the locked stub code prints Valid if the username is valid; otherwise Invalid each on a new line.

Sample Input

8 Julia Samantha Samantha_21 1Samantha Samantha?10_2A JuliaZ007 Julia@007 _Julia007 {codeBox}

Sample Output

Invalid Valid Valid Invalid Invalid Valid Invalid Invalid {codeBox}

Explanation

Refer diagram in the challenge statement.

Java Valid Username Regular Expression - Hacker Rank Solution

Approach I:

import java.util.Scanner;
class UsernameValidator {
    public static final String regularExpression = "\\p{Alpha}(\\w|_){7,29}";
}


public class Solution {
    private static final Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        int n = Integer.parseInt(scan.nextLine());
        while (n-- != 0) {
            String userName = scan.nextLine();

            if (userName.matches(UsernameValidator.regularExpression)) {
                System.out.println("Valid");
            } else {
                System.out.println("Invalid");
            }           
        }
    }
}


Approach II:

import java.util.Scanner;
class UsernameValidator {
    public static final String regularExpression = "^[A-Za-z]\\w{7,29}$";
}


public class Solution {
    private static final Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        int n = Integer.parseInt(scan.nextLine());
        while (n-- != 0) {
            String userName = scan.nextLine();

            if (userName.matches(UsernameValidator.regularExpression)) {
                System.out.println("Valid");
            } else {
                System.out.println("Invalid");
            }           
        }
    }
}


Approach III:

import java.util.Scanner;
class UsernameValidator {
    public static final String regularExpression = "[a-zA-Z](\\w){7,29}";
}


public class Solution {
    private static final Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        int n = Integer.parseInt(scan.nextLine());
        while (n-- != 0) {
            String userName = scan.nextLine();

            if (userName.matches(UsernameValidator.regularExpression)) {
                System.out.println("Valid");
            } else {
                System.out.println("Invalid");
            }           
        }
    }
}

Also Read: 

Disclaimer: The above Problem ( Java Valid Username Regular Expression ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these solutions will pass all the test cases. Now visit Java Valid Username Regular Expression HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post