Java (1) Prompt the user to enter a string of their choosing. Store the text in
ID: 3872032 • Letter: J
Question
Java
(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)
Ex:
(2) Implement a printMenu() method, which outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to call printMenu() until the user enters q to Quit. (3 pts)
Ex:
(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number of characters in the string, excluding all whitespace. Call getNumOfNonWSCharacters() in the main() method. (4 pts)
Ex:
(4) Implement the getNumOfWords() method. getNumOfWords() has a string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call getNumOfWords() in the main() method. (3 pts)
Ex:
(5) Implement the findText() method, which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The method returns the number of instances a word or phrase is found in the string. In the main() method, prompt the user for a word or phrase to be found and then call findText() in the main() method. (3 pts)
Ex:
(6) Implement the replaceExclamation() method. replaceExclamation() has a string parameter and returns a string which replaces each '!' character in the string with a '.' character. replaceExclamation() DOES NOT output the string. Call replaceExclamation() in the main() method, and then output the edited string. (3 pts)
Ex.
(7) Implement the shortenSpace() method. shortenSpace() has a string parameter and returns a string that replaces all sequences of 2 or more spaces with a single space. shortenSpace() DOES NOT output the string. Call shortenSpace() in the main() method, and then output the edited string. (3 pt)
Ex:
this my code:
import java.util.Scanner;
public class AuthoringAssistant {
public static int getNumOfWords(final String usrStr) {
int count = 0;
int i = 0;
for (i = 1; i < usrStr.length(); ++i) {
if ( (Character.isWhitespace(usrStr.charAt(i)) == true) &&
(Character.isWhitespace(usrStr.charAt(i - 1))) == false) {
++count;
}
}
if (Character.isWhitespace(usrStr.charAt(i - 1)) == false) {
++count;
}
return count;
}
public static int getNumOfNonWSCharacters(final String usrStr) {
int count = 0;
int i = 0;
//FIX ME! use a for loop to check for whitespace in string and increment count
for(i=0; i < usrStr.length(); i++){
if (usrStr.charAt(i) != ' '){
++count;
}
}
return count;
}
public static int findText(final String toFind, String usrStr) {
int count = 0;
int where = 0;
// FIX ME: Use a do-while loop which exits when the variable where is equal to a -1
do {// Begin do-while
//
where = usrStr.indexOf(toFind);
if (where == -1) {
return count;
}
//
count++;
usrStr = usrStr.substring(where + 1, usrStr.length());
}while(where >= 0); // End do-while
return count;
}
public static String replaceExclamation(String usrStr) {
return usrStr.replace('!', '.');
}
public static String shortenSpace(String usrStr) {
// Check for occurances of two spaces
while (usrStr.indexOf(" ") != -1) {
// FIX ME: Replace two spaces with a single space in userStr
usrStr=usrStr.replace(" ", " ");
}
return usrStr;
}
public static char printMenu(Scanner scnr) {
char menuOp = ' ';
System.out.println(" MENU");
// FIX ME: Display rest of menu to match required output
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.print(" ");
while (menuOp != 'c' && menuOp != 'w' && menuOp != 'f' &&
menuOp != 'r' && menuOp != 's' && menuOp != 'o' &&
menuOp != 'q') {
System.out.println( "Choose an option:");
menuOp = scnr.nextLine().charAt(0);
}
return menuOp;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputString = "";
char menuChoice = ' ';
String toFind = "";
// Get string from user
System.out.println("Enter a sample text:");
inputString = scnr.nextLine();
System.out.println(" You entered: " + inputString);
// FIX ME! Complete the while loop: test for menuChoice and exit loop when menuChoice = 'q'
while (menuChoice != 'q'){
menuChoice = printMenu(scnr);
if (menuChoice == 'c') {
System.out.println("Number of non-whitespace characters: " + getNumOfNonWSCharacters(inputString));
// menuChoice = ' ';
}// End first if
// FIX ME: Test for other menuChoice options
else if (menuChoice == 'w') {
System.out.println("Number of words: " + getNumOfNonWSCharacters(inputString));
}
else if (menuChoice == 'f'){
System.out.println("Enter a word or phrase to be found:");
toFind = scnr.nextLine();
System.out.println(""" + toFind + "" instances: " + findText(toFind, inputString));
}
else if (menuChoice == 'r') {
System.out.println("Edited text: " + replaceExclamation(inputString));
}
else if (menuChoice == 's') {
System.out.println("Edited text: " + shortenSpace(inputString));
}
return;
}// End main.
}// End class.
}
Explanation / Answer
Given below is the fixed code. In case of any issues, post a comment and I'll help. It the answer helped, please rate it. Thank you.
import java.util.Scanner;
public class AuthoringAssistant {
public static int getNumOfWords(final String usrStr) {
int count = 1;//there is atleast one word
int i = 0;
for (i = 1; i < usrStr.length(); ++i) {
if ( (Character.isWhitespace(usrStr.charAt(i)) == true) &&
(Character.isWhitespace(usrStr.charAt(i - 1))) == false) {
++count;
}
}
return count;
}
public static int getNumOfNonWSCharacters(final String usrStr) {
int count = 0;
int i = 0;
//FIX ME! use a for loop to check for whitespace in string and increment count
for(i=0; i < usrStr.length(); i++){
if (!Character.isWhitespace(usrStr.charAt(i))){
++count;
}
}
return count;
}
public static int findText(final String toFind, String usrStr) {
int count = 0;
int where = 0;
// FIX ME: Use a do-while loop which exits when the variable where is equal to a -1
do {// Begin do-while
//
where = usrStr.indexOf(toFind);
if (where != -1)
count++;
usrStr = usrStr.substring(where + toFind.length() - 1);
}while(where != -1); // End do-while
return count;
}
public static String replaceExclamation(String usrStr) {
return usrStr.replaceAll("!", ".");
}
public static String shortenSpace(String usrStr) {
// Check for occurances of two spaces
String space = " ";
String space2 = space + space;
while (usrStr.indexOf(space2) != -1) {
// FIX ME: Replace two spaces with a single space in userStr
usrStr=usrStr.replace(space2, space);
}
return usrStr;
}
public static char printMenu(Scanner scnr) {
char menuOp = ' ';
System.out.println(" MENU");
// FIX ME: Display rest of menu to match required output
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.print(" ");
while (menuOp != 'c' && menuOp != 'w' && menuOp != 'f' &&
menuOp != 'r' && menuOp != 's' && menuOp != 'o' &&
menuOp != 'q') {
System.out.println( "Choose an option:");
menuOp = scnr.nextLine().charAt(0);
}
return menuOp;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputString = "";
char menuChoice = ' ';
String toFind = "";
// Get string from user
System.out.println("Enter a sample text:");
inputString = scnr.nextLine();
System.out.println(" You entered: " + inputString);
// FIX ME! Complete the while loop: test for menuChoice and exit loop when menuChoice = 'q'
while (menuChoice != 'q'){
menuChoice = printMenu(scnr);
if (menuChoice == 'c') {
System.out.println("Number of non-whitespace characters: " + getNumOfNonWSCharacters(inputString));
// menuChoice = ' ';
}// End first if
// FIX ME: Test for other menuChoice options
else if (menuChoice == 'w') {
System.out.println("Number of words: " + getNumOfWords(inputString));
}
else if (menuChoice == 'f'){
System.out.println("Enter a word or phrase to be found:");
toFind = scnr.nextLine();
System.out.println(""" + toFind + "" instances: " + findText(toFind, inputString));
}
else if (menuChoice == 'r') {
System.out.println("Edited text: " + replaceExclamation(inputString));
}
else if (menuChoice == 's') {
System.out.println("Edited text: " + shortenSpace(inputString));
}
}
}// End main.
}// End class.
output
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
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:
c
Number of non-whitespace characters: 181
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:
w
Number of words: 35
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:
f
Enter a word or phrase to be found:
more
"more" instances: 5
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: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue.
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: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
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:
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.