(1) Prompt the user to enter a string of their choosing. Store the text in a str
ID: 3711885 • Letter: #
Question
(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)
Explanation / Answer
Let me know if you have any doubt.
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String mainString = inputString();
char menuChoice = '?';
while(menuChoice != 'q'){
menuChoice = printMenu();
if(menuChoice == 'c'){
int n = getNumOfNonWSCharacters(mainString);
}
else if(menuChoice == 'w'){
int n = getNumOfWords(mainString);
}
else if(menuChoice == 'f'){
System.out.print("Enter the word or phrase to be found : ");
String findText = scnr.next();
int n = findText(mainString,findText);
}
else if(menuChoice == 'r'){
String output = replaceExclamation(mainString);
System.out.println("Edited string is : "+output);
}
else if(menuChoice == 's'){
String output = shortenSpace(mainString);
System.out.println("Edited string is : "+output);
}
else if(menuChoice == 'q'){
System.out.println("Goodbye!!!");
}
else{
System.out.println("You entered a wrong option.Please select the correct option again!!!");
}
}
}
public static String inputString(){
/*
Prompt the user to enter a string of their choosing.Store the text in a string.Output the string.
*/
Scanner scnr = new Scanner(System.in);
String mainString = "";
System.out.print("Enter the string : ");
mainString = scnr.nextLine();
System.out.print("You entered: ");
System.out.println(mainString);
return mainString;
}
public static char printMenu(){
/*
Implement a printMenu() method, which outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option.
*/
Scanner scnr = new Scanner(System.in);
char menuChoice;
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.print("Enter your choice : ");
menuChoice = scnr.next().charAt(0);
return menuChoice;
}
public static int getNumOfNonWSCharacters(String mainString){
/*
Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number of characters in the string, excluding all whitespace.
*/
char characters[] = mainString.toCharArray();
int numOfNonWSChars = 0;
for(int i=0;i<mainString.length();i++){
if(characters[i]==' '){
continue;
}
else{
numOfNonWSChars++;
}
}
System.out.println("The number of non-whitespace characters in the string : " + numOfNonWSChars);
return numOfNonWSChars;
}
public static int getNumOfWords(String mainString){
/*
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.
*/
char characters[] = mainString.toCharArray();
int numOfWords = 0;
boolean flag = true;
for(int i=0;i<mainString.length();i++){
if(flag==true&&characters[i]!=' '){
flag = false;
numOfWords++;
continue;
}
else{
if(characters[i]==' '&&flag==false){
flag = true;
}
else{
continue;
}
}
}
System.out.println("The number of words in the string : " + numOfWords);
return numOfWords;
}
public static int findText(String mainString,String findText){
/*
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.
*/
int count = mainString.replaceAll("[^" + findText + "]", "").length() / findText.length();
if(count>0){
System.out.println(findText+" occurs "+count+" times in "+mainString);
}
else{
System.out.println(findText+" didn't occur in "+mainString);
}
return count;
}
public static String replaceExclamation(String mainString){
/*
Implement the replaceExclamation() method. replaceExclamation() has a string parameter and returns a string which replaces each '!' character in the string with a '.' character.
*/
String output = "";
char characters[] = mainString.toCharArray();
for(int i=0;i<mainString.length();i++){
if(characters[i]=='!'){
output = output + '.';
}
else{
output = output + characters[i];
}
}
return output;
}
public static String shortenSpace(String mainString){
/*
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.
*/
String output = mainString.replaceAll("\s{2,}", " ");
return output;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.