Advertisement
JennyDi

Username

Apr 3rd, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package final_exam_prep;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Username {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String input = scanner.nextLine();
  10.         String command = scanner.nextLine();
  11.         while (!command.equals("Sign up")) {
  12.             String[] commands = command.split("\\s+");
  13.             switch (commands[0]) {
  14.  
  15.                 case "Case": {
  16.                     if (commands[1].equals("upper")) {
  17.                         input = input.toUpperCase();
  18.                         System.out.println(input);
  19.                     } else if (commands[1].equals("lower")){
  20.                         input = input.toLowerCase();
  21.                         System.out.println(input);
  22.                     } else {
  23.                         continue;
  24.                     }
  25.                 }
  26.                 break;
  27.  
  28.                 case "Reverse": {
  29.                     int start = Integer.parseInt(commands[1]);
  30.                     int end = Integer.parseInt(commands[2]);
  31.                     String substring = "";
  32.                     if (notOutOfBounds(start, end + 1, input.length())) {
  33.                         substring = input.substring(start, end + 1);
  34.  
  35.                         StringBuilder sb = new StringBuilder();
  36.                         for (int i = substring.length() - 1; i >= 0; i--) {
  37.                             String letter = String.valueOf(substring.charAt(i));
  38.                             sb.append(letter);
  39.                         }
  40.                         System.out.println(sb.toString());
  41.                     }
  42.                 }
  43.                 break;
  44.  
  45.                 case "Cut": {
  46.                     String substring = commands[1];
  47.                     if (input.contains(substring)) {
  48.                         System.out.println(input.replace(substring, ""));
  49.                     } else {
  50.                         System.out.printf("The word %s doesn't contain %s.%n", input, substring);
  51.                     }
  52.                 }
  53.                 break;
  54.  
  55.                 case "Replace": {
  56.                     char symbol = commands[1].charAt(0);
  57.                     input = input.replaceAll(String.valueOf(symbol), "*");
  58.                     System.out.println(input);
  59.                 }
  60.                 break;
  61.  
  62.                 case "Check": {
  63.                     char symbol = commands[1].charAt(0);
  64.                     if (input.contains(String.valueOf(symbol))) {
  65.                         System.out.println("Valid");
  66.                     } else {
  67.                         System.out.printf("Your username must contain %c.%n", symbol);
  68.                     }
  69.                 }
  70.                 break;
  71.  
  72.             }
  73.             command = scanner.nextLine();
  74.         }
  75.     }
  76.  
  77.     public static boolean notOutOfBounds(int index, int index2, int size) {
  78.         if (index >= 0 && index < size && index2 >= 0 && index2 < size) {
  79.             return true;
  80.         }
  81.         return false;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement