Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. package ExamsTasks;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class TheFinalQuest {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. List<String> input = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  11.  
  12. String command = scanner.nextLine();
  13. while (!"Stop".equals(command)) {
  14. String[] differentComands = command.split(" ");
  15. String typeComand = differentComands[0];
  16.  
  17.  
  18. if (typeComand.equals("Delete")) {
  19. int index = Integer.parseInt(differentComands[1]) + 1;
  20. if (index >= 0 && index < input.size()) {
  21. input.remove(index);
  22. }
  23. } else if (typeComand.equals("Swap")) {
  24. String firstWord = differentComands[1];
  25. String secWord = differentComands[2];
  26.  
  27. if (input.contains(firstWord) && input.contains(secWord)) {
  28. int firstIndx = input.indexOf(firstWord);
  29. int secIndx = input.indexOf(secWord);
  30.  
  31. input.set(firstIndx, secWord);
  32. input.set(secIndx, firstWord);
  33. }
  34.  
  35. } else if (typeComand.equals("Put")) {
  36. String wordPut = differentComands[1];
  37. int indexPut = Integer.parseInt(differentComands[2]) - 1;
  38. if (indexPut >= 0 && indexPut <= input.size()) {
  39. input.add(indexPut, wordPut);
  40. }
  41.  
  42. } else if (typeComand.equals("Sort")) {
  43. Collections.reverse(input);
  44. } else if (typeComand.equals("Replace")) {
  45. String replacingWord = differentComands[1];
  46. String collectionWord = differentComands[2];
  47.  
  48. if (input.contains(collectionWord)) {
  49. int indexCollectWord = input.indexOf(collectionWord);
  50. input.set(indexCollectWord, replacingWord);
  51. }
  52.  
  53. }
  54. command = scanner.nextLine();
  55. }
  56.  
  57. for (String s : input) {
  58. System.out.print(s + " ");
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement