Username<\/strong><\/td>Validity<\/strong><\/td><\/tr>Julia<\/td> | INVALID; Username length < 8 characters<\/td><\/tr> | Samantha<\/td> | VALID<\/td><\/tr> | Samantha_21<\/td> | VALID<\/td><\/tr> | 1Samantha<\/td> | INVALID; Username begins with non-alphabetic character<\/td><\/tr> | Samantha?10_2A<\/td> | INVALID; ‘?’ character not allowed<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n Update the value of regularExpression field in the UsernameValidator class so that the regular expression only matches with valid usernames.<\/p>\n\n\n\n <\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line of input contains an integer n<\/em><\/strong>, describing the total number of usernames. Each of the next n <\/em><\/strong>lines contains a string describing the username. The locked stub code reads the inputs and validates the username.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h4>\n\n\n\n- 1 <= n <= 100<\/strong><\/li>
- The username consists of any printable characters.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nFor each of the usernames, the locked stub code prints Valid if the username is valid; otherwise Invalid each on a new line.<\/p>\n\n\n\n <\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n 8\n Julia\n Samantha\n Samantha_21\n 1Samantha\n Samantha?10_2A\n JuliaZ007\n Julia@007\n _Julia007<\/code><\/pre>\n\n\n\n<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n Invalid\n Valid\n Valid\n Invalid\n Invalid\n Valid\n Invalid\n Invalid<\/code><\/pre>\n\n\n\n<\/span>Solution –<\/strong> Valid Username Regular Expression<\/strong> in Java<\/strong><\/span><\/h2>\n\n\n\nimport java.util.Scanner;\nclass UsernameValidator {\n \/*\n * Write regular expression here.\n *\/\n public static final String regularExpression = \"^[A-Za-z]\\\\w{7,29}$\";\n}\n\n\npublic class Solution {\n private static final Scanner scan = new Scanner(System.in);\n \n public static void main(String[] args) {\n int n = Integer.parseInt(scan.nextLine());\n while (n-- != 0) {\n String userName = scan.nextLine();\n\n if (userName.matches(UsernameValidator.regularExpression)) {\n System.out.println(\"Valid\");\n } else {\n System.out.println(\"Invalid\");\n } \n }\n }\n}<\/pre>\n\n\n\n
| | |