Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> words = Arrays.stream(scanner.nextLine().split(" "))
- .collect(Collectors.toList());
- String inputCommand = scanner.nextLine();
- String second = "";
- String first = "";
- System.out.println();
- while (!inputCommand.equals("Stop")) {
- List<String> commands = Arrays.stream(inputCommand.split("\\s+"))
- .collect(Collectors.toList());
- if (commands.size() == 2){
- first = commands.get(1);
- }
- if (commands.size() == 3){
- first = commands.get(1);
- second = commands.get(2);
- }
- if (commands.contains("Delete")) {
- int indexDelete = Integer.parseInt(first);
- if ((words.size() > indexDelete + 1) && (indexDelete + 1 >= 0)){
- words.remove((Integer.parseInt(first) + 1));
- }
- }
- if (commands.contains("Replace")) {
- if (words.contains(second)) {
- words.set(words.indexOf(second), first);
- }
- }
- if (commands.contains("Swap")) {
- if (words.contains(first) && words.contains(second)) {
- Collections.swap(words,words.indexOf(first),words.indexOf(second));
- }
- }
- if (commands.contains("Put")) {
- int firstInt = Integer.parseInt(second);
- if (((firstInt - 1) >= 0) && ((firstInt - 1) <= words.size())){
- words.add(firstInt-1,first);
- }
- }
- if (commands.contains("Sort")) {
- Collections.sort(words);
- Collections.reverse(words);
- }
- inputCommand = scanner.nextLine();
- }
- System.out.println(words.toString().replaceAll("[\\[\\],]", ""));
- }
- }
Add Comment
Please, Sign In to add comment