RGAlex

O1. Password Reset / FinalExam - O4

Mar 16th, 2021 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. package FinalExam.O4;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class O1PasswordReset {
  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 (!"Done".equals(command)) {
  12.             String[] type = command.split("\\s+");
  13.             switch (type[0]) {
  14.                 case "TakeOdd":
  15.                     StringBuilder test = new StringBuilder();
  16.                     for (int i = 0; i < input.length(); i++) {
  17.                         if (i % 2 > 0) {
  18.                             test.append(input.charAt(i));
  19.                         }
  20.                     }
  21.                     input = new StringBuilder(test);
  22.                     System.out.println(input);
  23.                     break;
  24.                 case "Cut":
  25.                     int from = Integer.parseInt(type[1]);
  26.                     int to = Integer.parseInt(type[2]);
  27.                     input.delete(from, from + to);
  28.                     System.out.println(input);
  29.                     break;
  30.                 case "Substitute":
  31.                     if (input.toString().contains(type[1])) {
  32.                         input = new StringBuilder(input.toString().replace(type[1], type[2]));
  33.                         System.out.println(input);
  34.                     } else {
  35.                         System.out.println("Nothing to replace!");
  36.                     }
  37.                     break;
  38.             }
  39.             command = scanner.nextLine();
  40.         }
  41.         System.out.println("Your password is: " + input);
  42.     }
  43. }
  44.  
Add Comment
Please, Sign In to add comment