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...
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
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Design and implement a function which has one input parameter which is a number which is...
Design and implement a function which has one input parameter which is a number which is greater than 50, called num. Then the function will create a dictionary whose keys are 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9. Then the function calculates the values for each of the above keys. The value for a key is all the numbers between 2 and input “num” that are divisible by the key. The function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT