Java Lab: Authoring Assistant

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

import java.util.Scanner;

public class AuthoringAssistant {
public static Scanner scnr = new Scanner(System.in);

public static char printMenu() {

System.out.println("\nMENU"); // Prints out this whole menu with a new line before the entire thing --> \n
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:"); // This is needed so the user will know to choose an option from the menu.

char inputChoice = scnr.nextLine().charAt(0); //

return inputChoice;

}

public static int inputString) {

inputString = inputString.trim().replaceAll("\\s", ""); // --> \\s means any number of whitespace characters.
// .trim()is a built-in function that eliminates leading and trailing spaces.
// replaceAll("\\s", "") is replacing all whitespace characters with nothing so you can get a compact string.
/*
x = x.replaceAll("\\s", "");
x = x.replaceAll("\\s+", "");

\\s - matches single whitespace character
\\s+ - matches sequence of one or more whitespace characters.
*/
return inputString.length(); // returns the number of characters only, without the white spaces.
}

public static int getNumOfWords(String inputString) {

String[] words = inputString.split("\\s+");
/*
The split function breaks a string into an array of substrings .
*/
// In this code we are breaking the inputString based on the number of spaces it contains (spaces are removed).
// The split function will return those words and will be the number of elements in words based on how much words
// Separated by spaces there are.
return words.length; // .length takes a number rather than a string length().

}

public static int findText(String toFind, String inputString) {
// toFind = the word the user wants the program to find, inputString = the sentence the user inputed.
int lastIndex = 0;
int toFindCount = 0;

while (lastIndex != -1) { // The while loop keeps running until the last index value is not equal to -1.

lastIndex = inputString.indexOf(toFind, lastIndex);
/* What this code is doing is that it start at index zero and end at the first occurrence of the word, adds toFindCount
* by 1, and then gets a new last index to find the next occurrence of the word if there is one.
*/

// indexOf(characters or character, starting index); --> returns the index number of a specified starting index to
// a string of character or just one character (but only up the first instance of a character or characters).
/*
public class test {
public static void main(String args[]) {

String userText = "heelp mee!";

int index = userText.indexOf("p mee", 2);

System.out.print(index);
}
}
Output: 4 (starts at index 2 and stops at index p)
*/

/*
toFind = A String value, representing the string to search for.
LastIndex = An int value, representing the index position to start the search from.
*/

if (lastIndex != -1) { // The indexOf function will return a -1 when it cannot find the specified character
toFindCount++; // Adds toFindCouny by 1 each time it hits this part.
lastIndex += toFind.length(); // Adds the last index with the length of the word the user wants to find
}
}

return toFindCount; // returns the number of words that were found.

}

public static String replaceExclamation(String inputString) {

String newStringE = inputString.replaceAll("!", "."); // If there is an exclamation in the string, it replaces all of it
with a period.
return newStringE;

}

public static String shortenSpace(String inputString) {
String newStringS = inputString.replaceAll("\\s{2, }", " ").trim();
// trim() = removes whitespaces on both ends of a string
return newStringS;

}

public static void main(String[] args) { // The program begins reading here from the main method.

String inputString;
String toFind;
String newStringE;
String newStringS;
char inputChoice;
int numNonWSChar;
int numWords;
int toFindCount;

System.out.println("Enter a sample text:\n"); // The program begins by prompting the user to enter a sample text with a new
line after this statement.
inputString = scnr.nextLine(); // It saves that entire linre text into inputString.

System.out.println("You entered: " + inputString); // The tells the user what he, she or t entered.

/* The do loop is always followed by a while statement.
do {
*some code*
}

while (a condition is true)

The difference between this and a regular while loop (just while by it-self) is the the do loop is gauranteed to run
at least once since the program reads from top to bottom. However, for a while(a condition) loop, the code will not
read from top to bottom unless the condition is true within the parenthesis. That is the difference.
*/

do { // (Keep doing) ***

do { // (Keep doing) **

inputChoice = printMenu(); // Calls printMenu method above

}

while ((inputChoice != 'c') && (inputChoice != 'w') && (inputChoice != 'f') && (inputChoice != 'r') &&
(inputChoice != 's') && (inputChoice != 'q')); // (while on of these conditions is true) **

switch (inputChoice) { // The user has the choice from a number of cases

case 'c':
numNonWSChar = // Calls the method to get number of characters without
white spaces.
System.out.println("Number of non-whitespace characters: " + numNonWSChar);
break;

case 'w':
numWords = getNumOfWords(inputString); // Calls the method to get number of words without
// white spaces.
System.out.println("Number of words: " + numWords);
break;

case 'f':
System.out.println("Enter a word or phrase to be found:"); // Prompts the user to do this
toFind = scnr.nextLine(); // The user then enters it and gets saved into toFind as a string.
toFindCount = findText(toFind, inputString); // Calls findText, but it sends toFind and inputString to the method.
System.out.println("\"" + toFind + "\" instances: " + toFindCount);
break;

case 'r':
newStringE = // calls the function replaceExclamation and replaces exclamations,
System.out.println("Edited text: " + newStringE); // otherwise it returns the original string back.
break;

case 's':
newStringS = shortenSpace(inputString); // calls shortenSpace method
System.out.println("Edited text: " + newStringS + " "); // prints out new string with only single spaces.
break;

default: // If the case is anything else other then the listed, the while loop will terminate.
// However, it will go back to the very top do loop and prompt the user with the menu since that do-while
// loop has no been terminated which only works when the user types q.
break;

}

}

while (inputChoice != 'q'); // (while the inputChoice does not = q) ***
}
}

collegehelper
welcome to shbcf.ru