SvetlanPetrova

Valid Usernames SoftUni

Aug 9th, 2021
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String input = scanner.nextLine(); //"sh, too_long_username, !lleg@l ch@rs, jeffbutt"
  7.         String[] words = input.split(", ");
  8.  
  9.         for (String word : words) {
  10.             //проверка дали е валидна -> print
  11.             if(isValidWord(word)) {
  12.                 System.out.println(word);
  13.             }
  14.         }
  15.  
  16.     }
  17.  
  18.     private static boolean isValidWord (String word) {
  19.         //валидна дължина -> от 3 до 16
  20.         if (!(word.length() >= 3 && word.length() <= 16)) {
  21.             return false;
  22.         }
  23.         //валидно съдържание -> letters, numbers, hyphens(-) and underscores(_)
  24.         for (char symbol : word.toCharArray()) {
  25.             //проверка за буква или цифра или - или _
  26.             if(!(Character.isLetterOrDigit(symbol) || symbol == '-' || symbol == '_')) {
  27.                 return false;
  28.             }
  29.         }
  30.         return true;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment