Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /**
  2. *
  3. * @author Jacob
  4. *
  5. */
  6. import java.util.*;
  7. public class Assignment2 {
  8.  
  9. public static void main(String[] args) {
  10. Scanner input = new Scanner(System.in);
  11.  
  12. String choice = null;
  13. String mainSentence = null;
  14. String word = null;
  15. String word2 = null;
  16. String tempSentence;
  17. char letter;
  18. int index;
  19. int counter = 0;
  20. boolean quit = false;
  21.  
  22. System.out.println("Please enter a main sentence: ");
  23. mainSentence = input.nextLine();
  24.  
  25. while(quit == false) {
  26.  
  27. System.out.println("Please choose from one of the options below:\n A. Enter a new main sentence\n B. Find a string\n C. Find all incidents of a string\n D. Find and Replace the String\n"
  28. + " E. Replace all incidents of a string to another one\n F. Count the number of words\n G. Count the number of occurences of a letter\n H. Count the total number of letters\n"
  29. + " I. Delete all the occurences of a word\n J. Exit\n\n Enter your choice: " );
  30. choice = input.nextLine();
  31.  
  32. switch (choice) {
  33.  
  34. case "A":
  35.  
  36. System.out.println("Enter a new main sentence: ");
  37. mainSentence = input.nextLine();
  38.  
  39. System.out.println("The new main sentence is: " + mainSentence + "\n");
  40.  
  41. break;
  42.  
  43. case "B":
  44.  
  45. System.out.println("Enter a word you would like to find: ");
  46. word = input.nextLine();
  47.  
  48. System.out.println("The first occurance of " + word + " is at " + mainSentence.indexOf(word));
  49.  
  50. break;
  51.  
  52. case "C":
  53.  
  54. System.out.println("Enter a word you would like to search for: ");
  55. word = input.nextLine();
  56.  
  57. counter = mainSentence.indexOf(word);
  58. while (counter >= 0) {
  59. System.out.println(counter);
  60. counter = word.indexOf(word, counter + 1);
  61. }
  62.  
  63. System.out.println(word + " occurs " + counter + " times.\n");
  64.  
  65. break;
  66.  
  67. case "D":
  68.  
  69. System.out.println("Enter the string you would like to search for: ");
  70. word = input.nextLine();
  71. System.out.println("Enter the string you would like to replace it with: ");
  72. word2 = input.nextLine();
  73.  
  74. if(mainSentence.contains(word) == true) {
  75.  
  76. mainSentence = mainSentence.replaceFirst(word, word2);
  77.  
  78. System.out.println("The new main sentence is: " + mainSentence);
  79.  
  80. }
  81.  
  82. else {
  83.  
  84. System.out.println("Search string not found!\n");
  85.  
  86. }
  87.  
  88. break;
  89.  
  90. case "E":
  91.  
  92. System.out.println("Enter the word you would like to replace: ");
  93. word = input.nextLine();
  94. System.out.println("Enter what you would like to replace it with: ");
  95. word2 = input.nextLine();
  96. mainSentence = mainSentence.replaceAll(word, word2);
  97. System.out.println("The new sentence is " + mainSentence);
  98.  
  99. break;
  100.  
  101. case "F":
  102.  
  103. counter = mainSentence.length() - mainSentence.replace(" ", "").length() + 1;
  104. System.out.println("The number of words is: " + counter);
  105.  
  106.  
  107. break;
  108.  
  109. case "G":
  110.  
  111. System.out.println("Enter a letter to count the number of occurrences: ");
  112. letter = input.next().charAt(0);
  113.  
  114. for(int i = 0; i < mainSentence.length(); i++) {
  115.  
  116. if(mainSentence.charAt(i) == letter)
  117.  
  118. counter++;
  119.  
  120. }
  121.  
  122. System.out.println(letter + " occurs " + counter + " times");
  123.  
  124. break;
  125.  
  126. case "H":
  127.  
  128. tempSentence = mainSentence.replaceAll(" ", "");
  129. System.out.println("Your sentence has " + tempSentence.length() + " letters.");
  130.  
  131.  
  132. break;
  133.  
  134. case "I":
  135.  
  136. System.out.println("Please enter a word you would like to delete: ");
  137. word = input.nextLine();
  138. mainSentence = mainSentence.replaceAll(word, "");
  139. System.out.println("The new main sentence is:\n " + mainSentence);
  140.  
  141. break;
  142.  
  143. case "J":
  144.  
  145. quit = true;
  146. System.out.println("Goodbye!");
  147.  
  148. break;
  149.  
  150. }
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement