RGAlex

O1. Secret Chat / FinalExam - O3

Mar 16th, 2021 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. package FinalExam.O3;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class O1SecretChat {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         StringBuilder input = new StringBuilder(scanner.nextLine());
  9.         String command = scanner.nextLine();
  10.  
  11.         while (!command.equals("Reveal")) {
  12.             String[] type = command.split(":\\|:");
  13.             switch (type[0]) {
  14.                 case "ChangeAll":
  15.                     input = new StringBuilder(input.toString().replace(type[1], type[2]));
  16.                     System.out.println(input);
  17.                     break;
  18.                 case "Reverse":
  19.                     if (input.toString().contains(type[1])) {
  20.  
  21.                         input.delete(input.indexOf(type[1]), input.indexOf(type[1]) + type[1].length())
  22.                                 .append(new StringBuilder(type[1]).reverse());
  23.  
  24.                         System.out.println(input);
  25.                     } else {
  26.                         System.out.println("error");
  27.                     }
  28.                     break;
  29.                 case "InsertSpace":
  30.                     input.insert(Integer.parseInt(type[1]), " ");
  31.                     System.out.println(input);
  32.                     break;
  33.             }
  34.             command = scanner.nextLine();
  35.         }
  36.         System.out.println("You have a new text message: " + input);
  37.     }
  38. }
  39.  
Add Comment
Please, Sign In to add comment