Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ExamsTasks;
- import java.util.*;
- import java.util.stream.Collectors;
- public class TheFinalQuest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> input = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!"Stop".equals(command)) {
- String[] differentComands = command.split(" ");
- String typeComand = differentComands[0];
- if (typeComand.equals("Delete")) {
- int index = Integer.parseInt(differentComands[1]) + 1;
- if (index >= 0 && index < input.size()) {
- input.remove(index);
- }
- } else if (typeComand.equals("Swap")) {
- String firstWord = differentComands[1];
- String secWord = differentComands[2];
- if (input.contains(firstWord) && input.contains(secWord)) {
- int firstIndx = input.indexOf(firstWord);
- int secIndx = input.indexOf(secWord);
- input.set(firstIndx, secWord);
- input.set(secIndx, firstWord);
- }
- } else if (typeComand.equals("Put")) {
- String wordPut = differentComands[1];
- int indexPut = Integer.parseInt(differentComands[2]) - 1;
- if (indexPut >= 0 && indexPut <= input.size()) {
- input.add(indexPut, wordPut);
- }
- } else if (typeComand.equals("Sort")) {
- Collections.reverse(input);
- } else if (typeComand.equals("Replace")) {
- String replacingWord = differentComands[1];
- String collectionWord = differentComands[2];
- if (input.contains(collectionWord)) {
- int indexCollectWord = input.indexOf(collectionWord);
- input.set(indexCollectWord, replacingWord);
- }
- }
- command = scanner.nextLine();
- }
- for (String s : input) {
- System.out.print(s + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement