Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. package FinalExam;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class First {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String string = scanner.nextLine();
  10.  
  11.  
  12. String input = scanner.nextLine();
  13.  
  14. while (!"Done".equals(input)) {
  15.  
  16. String[] tokens = input.split("\\s+");
  17. String commander = tokens[0];
  18.  
  19. switch (commander) {
  20.  
  21. case "TakeOdd": {
  22. String temporary = "";
  23.  
  24. for (int i = 1; i < string.length(); i += 2) {
  25. temporary += string.charAt(i);
  26. }
  27.  
  28. string = temporary;
  29.  
  30. System.out.println(string);
  31.  
  32. break;
  33. }
  34. case "Cut": {
  35. int index = Integer.parseInt(tokens[1]);
  36. int length = Integer.parseInt(tokens[2]);
  37.  
  38. String firstPart = string.substring(0, index);
  39. String secondPart = string.substring(index + length);
  40.  
  41. string = firstPart + secondPart;
  42.  
  43. System.out.println(string);
  44.  
  45. break;
  46. }
  47. case "Substitute": {
  48. String searchFor = tokens[1];
  49. String replaceWith = tokens[2];
  50. String temporary = "";
  51.  
  52. if (string.contains(String.valueOf(searchFor))) {
  53.  
  54.  
  55. temporary = string.replaceAll(searchFor, replaceWith);
  56. string = temporary;
  57.  
  58. System.out.println(string);
  59. } else {
  60. System.out.println("Nothing to replace!");
  61. }
  62.  
  63. break;
  64. }
  65. }
  66. input = scanner.nextLine();
  67. }
  68. System.out.println("Your password is:" + " " + string);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement