Valid Username Regular Expression Hacker Rank Solution

Hello coders, In this post, you will learn how to solve Valid Username Regular Expression Hacker Rank Solution. This problem is a part of the Java programming series. 

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

Valid Username Regular Expression Hacker Rank Solution
Valid Username Regular Expression Hacker Rank Solution

Valid Username Regular Expression Hacker Rank Solution

Objective

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  to  characters inclusive. If the username consists of less than 8 or greater than 30 characters, then it is an invalid username.

Problem Statement: Click Here

Valid Username Regular Expression Hacker Rank Solution

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");
        }
    }
}

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

Sharing Is Caring

Leave a Comment