We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void validator(String word){
// First character must be a letter : ^[a-zA-Z]
// Following characters can be either letters , numbers or _ : [a-zA-Z_0-9]
// Must have 8-30 characters so after the first validation to the last character
// can have 7 to 29 = {7,29}$
if (word.matches("^[a-zA-Z][a-zA-Z_0-9]{7,29}$")) {
System.out.println("Valid");
}
else{
System.out.println("Invalid");
}
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int numWords = sc.nextInt();
sc.nextLine();
for (int i = 0; i < numWords; i++) {
String word = sc.nextLine();
validator(word);
}
sc.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Valid Username Regular Expression
You are viewing a single comment's thread. Return to all comments →
JAVA SOLUTION
import java.io.; import java.util.;
public class Solution {
}