KenDoBG

Untitled

Aug 14th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Username {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         StringBuilder username = new StringBuilder(scanner.nextLine());
  7.         String input = scanner.nextLine();
  8.         while (!input.equals("Registration")){
  9.             String command = input.split(" ")[0];
  10.             switch (command){
  11.                 case "Letters":
  12.                     String option = input.split(" ")[1];
  13.                     if (option.equals("Lower")){
  14.                         username = new StringBuilder (username.toString().toLowerCase());
  15.                     } else if (option.equals("Upper")){
  16.                         username = new StringBuilder (username.toString().toUpperCase());
  17.                     }
  18.                     System.out.println(username);
  19.                     break;
  20.                 case "Reverse":
  21.                     int startIndex = Integer.parseInt(input.split(" ")[1]);
  22.                     int endIndex = Integer.parseInt(input.split(" ")[2]);
  23.                     if (isValidIndex(startIndex,username) && isValidIndex(endIndex, username)){
  24.                         System.out.println(new StringBuilder(username.subSequence(startIndex, endIndex+1)).reverse());
  25.                     }
  26.                     break;
  27.                 case "Substring":
  28.                     String substring = input.split(" ")[1];
  29.                     if (username.toString().contains(substring)){
  30.                         username = new StringBuilder (username.toString().replace(substring, ""));
  31.                         System.out.println(username);
  32.                     } else {
  33.                         System.out.printf("The username %s doesn't contain %s.%n",username,substring);
  34.                     }
  35.                     break;
  36.                 case "Replace":
  37.                     char character = input.split(" ")[1].charAt(0);
  38.                     username = new StringBuilder (username.toString().replaceAll(String.valueOf(character), "-"));
  39.                     System.out.println(username);
  40.                     break;
  41.                 case "IsValid":
  42.                     character = input.split(" ")[1].charAt(0);
  43.                     if (username.toString().contains(String.valueOf(character))){
  44.                         System.out.println("Valid username");
  45.                     } else {
  46.                         System.out.printf("%c must be contained in your username.%n",character);
  47.                     }
  48.                     break;
  49.             }
  50.             input = scanner.nextLine();
  51.         }
  52.     }
  53.     public static boolean isValidIndex(int index, StringBuilder username){
  54.         return index >= 0 && index < username.length();
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment