Input Format

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


  • The username consists of any printable characters.

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.

hackerrank valid username regular expression solution in java

Valid username Regular expression problem solution in java | HackerRank

import java.util.Scanner;
class UsernameValidator {
     * Write regular expression here.
     public static final String regularExpression =

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

            if (userName.matches(UsernameValidator.regularExpression)) {
            } else {