Question

In: Computer Science

(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number...


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

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


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:

Number of words: 35

ANSWER IN JAVA more coming

Solutions

Expert Solution

import java.util.Scanner;
public class JavaCode
{
   private static Scanner scanner=new Scanner(System.in);
   public static void main(String[] args)
   {
       String text;
       System.out.println("Enter a sample text: ");
       sampletext=scanner.nextLine();
       System.out.println("You entered: "+sampletext);
       char choice=printMenu();
       switch(choice)
       {
       case 'c':
           int countNonWhitespaces=getNumOfNonWSCharacters(text);
           System.out.println("Number of non-whitespace characters: "+countNonWhitespaces);
           break;
       case 'w':
           int wCount=getNumOfWords(text);
           System.out.println("Number of words: "+wCount);
           break;
       case 'f':
           System.out.println("Enter a word or phrase to be found: ");
           String findword=scanner.nextLine();
           int findCount=findText(text,findword);
           System.out.println("Number of words: "+findCount);
           break;
       case 'r':
           String newstring=replaceExclamation(text);
           System.out.println("Edited text:"+newstring);
           break;
       case 's':
           newstring=shortenSpace(text);
           System.out.println("Edited text:"+newstring);
           break;
       case 'q':
           System.exit(0);
       }

   }

   private static String shortenSpace(String text) {
       String temp = text.trim().replaceAll(" +", " ");
       return temp;

   }
   private static String replaceExclamation(String text) {

       String temp = text.replaceAll("!", "a");
       return temp;
   }
   private static int findText(String text, String find)
   {
       String[] words=text.split(" ");

       int count=0;
       for (int i = 0; i < words.length; i++)
       {
           if(words[i].equals(find))
               count++;
       }

       return count;
   }

   private static int getNumOfWords(String text) {
       String[] words=text.split(" ");
       return words.length;
   }

   private static int getNumOfNonWSCharacters(String text) {

       text=text.trim().replaceAll("\\s","");
       return text.length();          
   }

   private static char printMenu()
   {


       System.out.println("\nMENU");
       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: ");


       char choice=scanner.nextLine().charAt(0);

       return choice;
   }
}

Related Solutions

Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both...
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both type string. This function checks for the name string in the contacts string and returns that person’s contact information. If the person is not found, the function returns, “name not in contact”. Assume input is always valid. Assume the same name is not repeated in contacts. [You may use split(), range(), len() ONLY and no other built-in function or method] Examples:               contacts = “Frank...
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns...
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns a list of strings - one element for each line in the file. These lines should have all the whitespace removed from both ends of the line. a. See the formatting of the individual_info data file. Consider how a file can be read into the program. In Python language
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both...
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both type string. This function checks for the name string in the contacts string and returns that person’s contact information. If the person is not found, the function returns, “name not in contact”. Assume input is always valid. Assume the same name is not repeated in contacts. [You may use split(), range(), len() ONLY and no other built-in function or method] Examples:               contacts = “Frank...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
Create a Java method that takes a String as input value and returns the number of...
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
How am I supposed to implement a c_str method that returns a c string representation of...
How am I supposed to implement a c_str method that returns a c string representation of a String object? I need to return a csting representation of a string object. I am confused on what cstrings are and where to go. method signature is as follows: char* c_str();
Design and implement a method that will take any value as String (even it's a number!)...
Design and implement a method that will take any value as String (even it's a number!) and convert that value to a number in any base. Use the method; private static ArrayList convertToBase(String value, int currentBase, int targetBase){ // your algorithm return result; //which is a String array } Examples: convertToBase("10011", 2, 10) should return [1, 9], meaning, (10011)base2 = (19)base10 convertToBase("100", 10, 8) should return [6, 4] convertToBase("E12B0", 16, 2) should return [1,1,1,0,0,0,0,1,0,0,1,0,1,0,1,1,0,0,0,0] convertToBase("1250", 10, 16) should return [4,...
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Create a method called firstLetter that takes a String parameter and integer parameter. It should return...
Create a method called firstLetter that takes a String parameter and integer parameter. It should return -1 if the number of words in the given String is greater than or equal to the integer parameter (it should return -1 and not process the String any further) (4 points). If the String does not have more words than the given integer, the method should return 1 if all the words in the string start with the same letter(8 points) and 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT