borovaneca

FinalQuest

Apr 6th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7. import java.util.List;
  8.  
  9. public class FinalQuest {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.  
  14.         List<String> message = new ArrayList<>(Arrays.asList(scanner.readLine().split("\\s")));
  15.  
  16.         String input;
  17.         while (!"Stop".equals(input = scanner.readLine())) {
  18.  
  19.             if (input.split("\\s")[0].equals("Delete")) {
  20.                 int index = Integer.parseInt(input.split("\\s")[1]);
  21.                 if (index + 1 >= 0 && index + 1 < message.size() && !message.isEmpty()) {
  22.                     message.remove(index+1);
  23.                 }
  24.              } else if (input.split("\\s")[0].equals("Swap")) {
  25.                 String word = input.split("\\s")[1].trim();
  26.                 String word2 = input.split("\\s")[2].trim();
  27.                 if (message.contains(word) && message.contains(word2)) {
  28.                     Collections.swap(message, message.indexOf(word), message.indexOf(word2));
  29.                 }
  30.             } else if (input.split("\\s")[0].equals("Put")) {
  31.                 int index = Integer.parseInt(input.split("\\s")[2]);
  32.                 if (index - 1 >= 0 && index - 1 <= message.size() - 1) {
  33.                     message.add(index - 1,  input.split("\\s")[1]);
  34.                 }
  35.  
  36.             } else if (input.split("\\s")[0].equals("Sort")) {
  37.                 Collections.sort(message);
  38.  
  39.             } else if (input.split("\\s")[0].equals("Replace")) {
  40.                 String first = input.split("\\s")[1].trim();
  41.                 String second = input.split("\\s")[2].trim();
  42.                 if (message.contains(second)) {
  43.                     message.set(message.indexOf(second), first);
  44.                 }
  45.             }
  46.         }
  47.         System.out.println(String.join(" ", message));
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment