desislava_topuzakova

01. Valid Usernames

Mar 14th, 2021
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.Scanner;
  3.  
  4. public class demo {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String text = scanner.nextLine();
  8.         //"sh, too_long_username, !lleg@l ch@rs, jeffbutt"
  9.         String[] usernames = text.split(", ");
  10.         //["sh", "too_long_username", "!lleg@l ch@rs", "jeffbutt"]
  11.  
  12.         for (String username : usernames) {
  13.             //ако е валиден -> печатаме го
  14.             if(isValidUsername(username)) {
  15.                 System.out.println(username);
  16.             }
  17.  
  18.         }
  19.  
  20.  
  21.  
  22.     }
  23.     static boolean isValidUsername (String username) {
  24.         //•   Has length between 3 and 16 characters -> [3;16]
  25.  
  26.         if (username.length() < 3 || username.length() > 16) {
  27.            return false;
  28.         }
  29.  
  30.         //•   Contains only letters, numbers, hyphens and underscores -> "Pesho"
  31.         for (int index = 0; index <= username.length() - 1; index++) {
  32.             char currentSymbol = username.charAt(index);
  33.             if(!Character.isLetterOrDigit(currentSymbol) && currentSymbol != '-' && currentSymbol != '_') {
  34.                 return false;
  35.             }
  36.         }
  37.  
  38.         return true;
  39.        
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment