Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class AuthoringAssistant {
- public static String shortenSpace(String singleSpace) {
- String doubleSpace = singleSpace.replaceAll(" ", " ");
- return doubleSpace;
- }
- public static String replaceExclamation(String exclaim) {
- String period = exclaim.replaceAll("!",".");
- return period;
- }
- public static int findText(String phrase, String sentence) {
- int phraseNum = 0;
- phraseNum = sentence.split(phrase).length - 1;
- return phraseNum;
- }
- public static int getNumOfWords(String wordCount) {
- int words = 0;
- //"a word is dead.
- for(int i = 0; i < wordCount.length(); i++) {
- if(wordCount.charAt(i) == ' ') {
- words = words + 1;
- }
- }
- words = words + 1;
- return words;
- }
- public static int getNumOfNonWSCharacters(String input) {
- int characterCount = 0;
- for(int i = 0; i < input.length(); i++) {
- if(input.charAt(i) != ' ') {
- characterCount = characterCount + 1;
- }
- }
- return characterCount;
- }
- public static String printMenu(){
- Scanner scnr = new Scanner(System.in);
- System.out.println("MENU");
- System.out.println("c - Number of non-whitespace characters");
- System.out.println("w - Number of words");
- System.out.println("f - Find text");
- System.out.println("r - Replace all !'s");
- System.out.println("s - Shorten spaces");
- System.out.println("q - Quit");
- System.out.println();
- System.out.println("Choose an option:");
- String input = scnr.next();
- while(!input.matches("c|w|f|r|s|q")) {
- input = scnr.next();
- }
- return input;
- }
- public static void main(String[] args) {
- Scanner scnr = new Scanner(System.in);
- System.out.println("Enter a sample text:");
- String sampleText = scnr.nextLine();
- System.out.println();
- System.out.println("You entered: " + sampleText);
- switch(printMenu()) {
- case "c":
- System.out.println("Number of non-whitespace characters: " + getNumOfNonWSCharacters(sampleText));
- break;
- case "w":
- System.out.println("Number of words: " + getNumOfWords(sampleText));
- break;
- case "f":
- System.out.println("Enter a word or phrase to be found: ");
- String phrase = scnr.nextLine();
- System.out.println("\"" + phrase + "\" instances: " + findText(phrase, sampleText));
- break;
- case "r":
- System.out.println("Edited text: " + replaceExclamation(sampleText));
- break;
- case "s":
- System.out.println(shortenSpace(sampleText));
- break;
- case "q":
- System.exit(0);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement