Advertisement
Guest User

Untitled

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