Java Authoring Assistant


#1
import java.util.Scanner;

public class AuthoringAssistant {

    public static Scanner scanner = new Scanner(System.in);

    public static int getNumOfNonWSCharacters(String text) {
        text = text.trim().replaceAll("\\s", "");
        return text.length();
    }

    public static int getNumOfWords(String text) {
        text = shortenSpace(text);
        String[] words = text.split(" ");
        return words.length;
    }

    public static int findText(String text, String find) {
        int counter = 0;
        int i = 0;

        while ((i = text.indexOf(find)) != -1) {
            text = text.substring(i + find.length());
            counter += 1;
        }
        return counter;
    }

    public static String replaceExclamation(String text) {
        String stRepEx = text.replaceAll("!", ".");
        return stRepEx;
    }

    public static String shortenSpace(String text) {
        String stRepSp = text.trim().replaceAll(" +", " ");
        return stRepSp;
    }

    public static void main(String[] args) {

        System.out.println("Enter a sample text:\n");
        String text = scanner.nextLine();

        System.out.println("You entered: " + text);
        while (true) {



            System.out.println("\nMENU");
            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("\nChoose an option:");

            char ch = scanner.nextLine().charAt(0);

            switch (ch) {
                case 'q':
                    System.exit(0);

                case 'c':
                    int cntNonWhiteSpaces = getNumOfNonWSCharacters(text);
                    System.out.println("Number of non-whitespace characters: " + cntNonWhiteSpaces);
                    break;

                case 'w':
                    int wordsCount = getNumOfWords(text);
                    System.out.println("Number of words: " + wordsCount);
                    break;

                case 'f':
                    System.out.println("Enter a word or phrase to be found: ");
                    String find = scanner.nextLine();
                    int findCount = findText(text, find);
                    System.out.println("\"" + find + "\" instances: " + findCount);
                    break;

                case 'r':
                    String newStr = replaceExclamation(text);
                    System.out.println("Edited text: " + newStr);
                    break;

                case 's':
                    newStr = shortenSpace(text);
                    System.out.println("Edited text:" + newStr);
                    break;

                default:
                    System.out.println("Invalid option. Please try again");
            }
        }
    }
}

Hey, all! I am doing a program in Java for an authoring assistant, and I am making headway with it, but I can’t get the updated result and use another option. For example, if I put in “Hello my name is Joey!” then put in s for the option to shorten the space, it will give me “Hello my name is Joey!” and then when I put in r to get rid of the exclamation, it gives me “Hello my name is Joey.” and doesn’t save the last result.

Output:

Enter a sample text:

Hello!    My name is Joey!
You entered: Hello!    My name is Joey!

MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit

Choose an option:
s
Edited text:Hello! My name is Joey!

MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit

Choose an option:
r
Edited text: Hello.    My name is Joey.

Can somebody please tell me what I’m doing wrong?


#2
case 's':
                    newStr = shortenSpace(text);
                    System.out.println("Edited text:" + newStr);
                    break;

You never modified the original string. You made a copy of it - modified that copy and used the copy in a print statement.

Your original string remains untouched

Hope this gets you in the right direction