Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class FinalQuest {
- public static void main(String[] args) throws IOException {
- BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
- List<String> message = new ArrayList<>(Arrays.asList(scanner.readLine().split("\\s")));
- String input;
- while (!"Stop".equals(input = scanner.readLine())) {
- if (input.split("\\s")[0].equals("Delete")) {
- int index = Integer.parseInt(input.split("\\s")[1]);
- if (index + 1 >= 0 && index + 1 < message.size() && !message.isEmpty()) {
- message.remove(index+1);
- }
- } else if (input.split("\\s")[0].equals("Swap")) {
- String word = input.split("\\s")[1].trim();
- String word2 = input.split("\\s")[2].trim();
- if (message.contains(word) && message.contains(word2)) {
- Collections.swap(message, message.indexOf(word), message.indexOf(word2));
- }
- } else if (input.split("\\s")[0].equals("Put")) {
- int index = Integer.parseInt(input.split("\\s")[2]);
- if (index - 1 >= 0 && index - 1 <= message.size() - 1) {
- message.add(index - 1, input.split("\\s")[1]);
- }
- } else if (input.split("\\s")[0].equals("Sort")) {
- Collections.sort(message);
- } else if (input.split("\\s")[0].equals("Replace")) {
- String first = input.split("\\s")[1].trim();
- String second = input.split("\\s")[2].trim();
- if (message.contains(second)) {
- message.set(message.indexOf(second), first);
- }
- }
- }
- System.out.println(String.join(" ", message));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment