Advertisement
goalCreateSupremeBot

Untitled

Apr 4th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10. String encrypted = scanner.nextLine();
  11. String command = scanner.nextLine();
  12. while (!command.equals("Finish")) {
  13. String[] commandParts = command.split("\\s+");
  14. String commandName = commandParts[0];
  15. switch (commandName) {
  16. case "Replace":
  17. encrypted = replace(encrypted, commandParts[1].charAt(0), commandParts[2].charAt(0));
  18. break;
  19. case "Cut":
  20. int startIndex = Integer.parseInt(commandParts[1]);
  21. int endIndex = Integer.parseInt(commandParts[2]);
  22. encrypted = cut(encrypted, startIndex, endIndex);
  23. }
  24. command = scanner.nextLine();
  25. }
  26. }
  27.  
  28. private static String cut(String current, int startIndex, int endIndex) {
  29. int length = current.length();
  30. if (!isValidIndex(startIndex, length)) {
  31. System.out.println("Invalid indexes!");
  32. return current;
  33. } else if (!isValidIndex(endIndex, length)) {
  34. System.out.println("Invalid indexes!");
  35. return current;
  36. }
  37. String firstPart = current.substring(0, startIndex);
  38. String secondPart = current.substring(endIndex + 1, length);
  39. String result = firstPart + secondPart;
  40. System.out.println(result);
  41. return result;
  42. }
  43.  
  44. private static boolean isValidIndex(int toCheck, int length) {
  45. return toCheck >= 0 && toCheck < length;
  46. }
  47.  
  48. public static String replace(String current, char searchFor, char replaceWith) {
  49. String result = current.replace(searchFor, replaceWith);
  50. System.out.println(result);
  51. return result;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement