Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class AuthoringAssistant {
  4. public static String shortenSpace(String singleSpace) {
  5. String doubleSpace = singleSpace.replaceAll(" ", " ");
  6.  
  7. return doubleSpace;
  8. }
  9.  
  10.  
  11. public static String replaceExclamation(String exclaim) {
  12. String period = exclaim.replaceAll("!",".");
  13.  
  14. return period;
  15. }
  16.  
  17.  
  18. public static int findText(String phrase, String sentence) {
  19. int phraseNum = 0;
  20.  
  21. phraseNum = sentence.split(phrase).length - 1;
  22.  
  23. return phraseNum;
  24. }
  25.  
  26.  
  27. public static int getNumOfWords(String wordCount) {
  28. int words = 0;
  29. //"a word is dead.
  30. for(int i = 0; i < wordCount.length(); i++) {
  31. if(wordCount.charAt(i) == ' ') {
  32. words = words + 1;
  33. }
  34. }
  35. words = words + 1;
  36. return words;
  37. }
  38.  
  39.  
  40. public static int getNumOfNonWSCharacters(String input) {
  41. int characterCount = 0;
  42.  
  43. for(int i = 0; i < input.length(); i++) {
  44. if(input.charAt(i) != ' ') {
  45. characterCount = characterCount + 1;
  46. }
  47. }
  48. return characterCount;
  49. }
  50.  
  51.  
  52. public static String printMenu(){
  53. Scanner scnr = new Scanner(System.in);
  54.  
  55. System.out.println("MENU");
  56. System.out.println("c - Number of non-whitespace characters");
  57. System.out.println("w - Number of words");
  58. System.out.println("f - Find text");
  59. System.out.println("r - Replace all !'s");
  60. System.out.println("s - Shorten spaces");
  61. System.out.println("q - Quit");
  62. System.out.println();
  63. System.out.println("Choose an option: ");
  64.  
  65. String input = scnr.nextLine();
  66. while(!input.matches("c|w|f|r|s|q")) {
  67. input = scnr.nextLine();
  68. if(input.equals("q")) {
  69. break;
  70. }
  71. }
  72.  
  73. return input;
  74. }
  75.  
  76. public static void main(String[] args) {
  77. Scanner scnr = new Scanner(System.in);
  78. System.out.println("Enter a sample text: ");
  79. String sampleText = scnr.nextLine();
  80. System.out.println();
  81. System.out.println("You entered: " + sampleText);
  82.  
  83. switch(printMenu()) {
  84.  
  85. case "c":
  86. System.out.println("Number of non-whitespace characters: " + getNumOfNonWSCharacters(sampleText));
  87. break;
  88. case "w":
  89. System.out.println("Number of words: " + getNumOfWords(sampleText));
  90. break;
  91. case "f":
  92. System.out.println("Enter a word or phrase to be found: ");
  93. String phrase = scnr.nextLine();
  94. System.out.println("\"" + phrase + "\" instances: " + findText(phrase, sampleText));
  95. break;
  96. case "r":
  97. System.out.println("Edited text: " + replaceExclamation(sampleText));
  98. break;
  99. case "s":
  100. System.out.println(shortenSpace(sampleText));
  101. break;
  102. case "q":
  103. System.exit(0);
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement