Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java CODE help please! (1) Prompt the user to enter a string of their choosing.

ID: 3581098 • Letter: J

Question

Java CODE help please!

(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:

import java.util.Scanner;

public class AuthoringAssistant {

public static void main(String[] args) {
/* Type your code here. */
  
return;
}
}

Explanation / Answer


// AuthoringAssistant.java

import java.util.Scanner;

public class AuthoringAssistant
{
private static String shortenSpace(String inputText)
{
String str = inputText.trim().replaceAll("\s+", " ");
return str;

}


private static String replaceExclamation(String inputText)
{
String str = inputText.replaceAll("!", ".");
return str;
}


private static int findText(String inputText, String findStr)
{
int c = 0;
int i;
while ((i = inputText.indexOf(findStr)) != -1)
{
inputText = inputText.substring(i + findStr.length());
c++;
}

return c;
}


private static int getNumOfWords(String inputText)
{
inputText = shortenSpace(inputText);
String[] w = inputText.split(" ");

return w.length;
}

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


private static void printMenu()
{
System.out.println(" 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: ");
}

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a sample text: ");
String inputText = scan.nextLine();
System.out.println("You entered: " + inputText);

while(true)
{
printMenu();
char ch = scan.nextLine().charAt(0);

switch (ch)
{

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

case 'w':   
int wc = getNumOfWords(inputText);
System.out.print(" Number of words: " + wc);
break;
  
case 's':   
String str = shortenSpace(inputText);
System.out.print(" Edited text:" + str+ " ");
break;

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


case 'f':   
System.out.println(" Enter a word or phrase to be found: ");
String findStr = scan.nextLine();
int count = findText(inputText, findStr);
System.out.print(""" + findStr + "" instances: " + count);
break;

case 'q':
System.exit(0);

default:
System.out.println("Invalid input. ");
}

System.out.println();
}
}
}

/*
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:
here
"here" instances: 2

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


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote