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

JAVA Code help if you can. My current code isn\'t working and really need any ki

ID: 3580007 • Letter: J

Question

JAVA Code help if you can. My current code isn't working and really need any kind of help here.

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

===================================================================================================

HERE'S MY CODE:

import java.util.Scanner;

public class AuthoringAssistant {

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

String returnString = "";
while(true){
printMenu();
System.out.println(" Choose an option: ");
char ch = scan.next().charAt(0);
if(ch == 'c' || ch == 'C'){
count = getNumOfNonWSCharacters(s);
System.out.println("Number of non-whitespace characters: "+count);
}
else if(ch == 'w' || ch == 'W'){
count = getNumOfWords(s);
System.out.println("Number of words: "+ count);
}
else if(ch == 'f' || ch == 'F'){
System.out.println("Enter a word or phrase to be found: ");
String find = scan.next();
count = findText(s, find);
System.out.println(""" + find + "" instances: "+ count);
}
else if(ch == 'r' || ch == 'R'){
returnString = replaceExclamation(s);
System.out.print("Edited text: " + returnString + ".");
}else if(ch == 's' || ch == 'S'){
returnString = shortenSpace(s);
System.out.println("Edited text: "+ returnString + ".");
}else if(ch == 'q' || ch == 'Q'){
break;
}   
}
return;
}
public 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");

}
public static int getNumOfNonWSCharacters(String s){
int count = 0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i) != ' '){
count++;
}
}
return count;
}
public static int getNumOfWords(String s){
int count = 0;
if(s.length()>0){
count = 1;
}
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == ' '){
count++;
}
}
return count;
}
public static int findText(String s, String find){
int count = 0;
String words[] = s.split("\s+");
for(int i=0; i<words.length; i++){
if(words[i].equalsIgnoreCase(find)){
count++;
}
}
return count;
}
public static String replaceExclamation(String s){
String returnString = "";
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == '!'){
returnString = returnString +"";
}
else{
returnString = returnString + s.charAt(i);
}
}

return returnString;
}
public static String shortenSpace(String s){
String returnString = "";
boolean isPreviousCharSpace = true;
for(int i=0; i<s.length(); i++){
if(s.charAt(i) != ' '){
isPreviousCharSpace = false;
}
if(isPreviousCharSpace == false){
returnString = returnString + s.charAt(i);
}
if(s.charAt(i) == ' '){
isPreviousCharSpace = true;
}

}
return returnString;
}

}
  

====================================================================================================

HERE'S MY OUTPUT:

Total: 10/20

1. Compare output

1/1

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! q

Enter a sample text: 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!

2. Compare output

2/2

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! q

Enter a sample text: 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:

3. Compare output

1/1

Testing that menu is called and can quit. q

Enter a sample text: You entered: Testing that menu is called and can quit. 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:

4. Unit test

2/2

Tests that getNumOfNonWSCharacters() returns 181 for "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!"

Your output

getNumOfNonWSCharacters() returns the correct amount.

5. Unit test

1/1

Tests that getNumOfNonWSCharacters() returns 12 for "This is a test."

Your output

getNumOfNonWSCharacters() returns the correct amount.

6. Compare output

1/1

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! c q

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:

7. Unit test

0/2

Tests that getNumOfWords() returns 35 for "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!"

Your output

getNumOfWords() incorrectly returned 41.

8. Compare output

0/1

This is a test. w q

Number of words: 6 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:

Number of words: 4 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:

9. Unit test

0/2

Tests that findText() returns 5 for parameters "more" and "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!"

Your output

findText() incorrectly returned 0.

10. Compare output

0/1

I want some water. I had some water earlier, but now he has some water. f some water q

umber of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit Choose an option: Number of words: 15 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:

Enter a word or phrase to be found: "some water" instances: 3 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:

11. Unit test

0/2

Test that replaceExclamation() works correctly with "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!"

Your output

replaceExclamation() incorrectly edits the string.

12. Compare output

0/1

I have a test! I am not prepared! r q

on: Edited text: I have a test I am not prepared. 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:

Edited text: I have a test. I am not prepared. 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:

13. Unit test

2/2

Tests that shortenSpace() removes extra spaces with "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!"

Your output

shortenSpace() returns the correct amount.

14. Compare output

0/1

There are too many spaces here. Why? s q

an option: Edited text: There are too many spaces here. Why? . 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:

Edited text: There are too many spaces here. Why? 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:

Input

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! q

Your output correctly starts with

Enter a sample text: 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!

Explanation / Answer

AuthoringAssistant.java

import java.util.Scanner;


public class AuthoringAssistant {
  
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
   System.out.print("Enter a sample text: ");
   String s = scan.nextLine();
   System.out.println("You entered: "+s);
   int count = 0;
     
   String returnString = "";
   while(true){
   printMenu();
   System.out.println(" Choose an option: ");
   char ch = scan.next().charAt(0);
   if(ch == 'c' || ch == 'C'){
       count = getNumOfNonWSCharacters(s);
       System.out.println("Number of non-whitespace characters: "+count);
   }
   else if(ch == 'w' || ch == 'W'){
       count = getNumOfWords(s);
       System.out.println("Number of words: "+count);
   }
else if(ch == 'f' || ch == 'F'){
   System.out.println("Enter a word or phrase to be found: ");
   String find = scan.next();
       count = findText(s, find);
       System.out.println("""+find+"" instances: "+count);
   }
else if(ch == 'r' || ch == 'R'){
   returnString = replaceExclamation(s);
   System.out.println("Edited text: "+returnString);
}else if(ch == 's' || ch == 'S'){
   returnString = shortenSpace(s);
   System.out.println("Edited text: "+returnString);
}else if(ch == 'q' || ch == 'Q'){
   break;
}     
   }
return;
}
public 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");
     
}
public static int getNumOfNonWSCharacters(String s){
   int count = 0;
   for(int i=0; i<s.length(); i++){
       if(s.charAt(i) != ' '){
           count++;
       }
   }
   return count;
}
public static int getNumOfWords(String s){
   int count = 0;
   if(s.length()>0){
       count = 1;
   }
   for(int i=0; i<s.length(); i++){
       if(s.charAt(i) == ' '){
           count++;
       }
   }
   return count;
}
public static int findText(String s, String find){
   int count = 0;
   String words[] = s.split("\s+");
   for(int i=0; i<words.length; i++){
       if(words[i].equalsIgnoreCase(find)){
           count++;
       }
   }
   return count;
}
public static String replaceExclamation(String s){
   String returnString = "";
   for(int i=0; i<s.length(); i++){
       if(s.charAt(i) == '!'){
           returnString = returnString +"";
       }
       else{
           returnString = returnString + s.charAt(i);
       }
   }
     
   return returnString;
}
public static String shortenSpace(String s){
   String returnString = "";
   boolean isPreviousCharSpace = true;
   for(int i=0; i<s.length(); i++){
       if(s.charAt(i) != ' '){
           isPreviousCharSpace = false;
       }
       if(isPreviousCharSpace == false){
           returnString = returnString + s.charAt(i);
       }
       if(s.charAt(i) == ' '){
           isPreviousCharSpace = true;
       }
         
   }
   return returnString;
}

}

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;
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;
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: 150
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: 34
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:
r
"r" instances: 0
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;
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