Question

Write an application that counts the words in a String entered by a user as part...

Write an application that counts the words in a String entered by a user as part of the main method. Words are separated by any combination of spaces, periods, commas, semicolons, question marks, exclamation points, or dashes. Save the file as CountWords.java.. Again, Don’t forget to create the application/project CountWordsTest.java Class that has the main method with input from the user and an object to use the CountWords class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

---------------------------------------CountWords.java-------------------------------------

class CountWords

{

    public int count(String str)

    {

        // split the string

        String[] arr = str.split(" |\\t|,|;|\\.|\\?|!|-|:|@|\\[|\\]|\\(|\\)|\\{|\\}|_|\\*|/");

   

        int i, x = 0;

       

        for( i = 0 ; i < arr.length ; i++ )

        {

            // if the length of current element is greater than 0

            if( arr[i].length() > 0 )

                x++;

        }

       

        return x;

    }

}

---------------------------------------------CountWordsTest.java------------------------------------

import java.util.*;

public class CountWordsTest

{

    public static void main(String[] args)

    {

        CountWords ob = new CountWords();

       

        // create a Scanner objects

        Scanner sc = new Scanner(System.in);

       

        System.out.print("Enter the string : ");

        String str = sc.nextLine();

       

        int x = ob.count(str);

       

        System.out.println("Number of words : " + x);

    }

}

Sample Output

C:AUsers\userDesktop javac CountWordsTest.java C:\Users\user\Desktop〉Java Coun two rdsTest Enter the string HiIaChandler Bing

Add a comment
Know the answer?
Add Answer to:
Write an application that counts the words in a String entered by a user as part...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java method that returns a String representing a file name entered by the user....

    Write a Java method that returns a String representing a file name entered by the user. Use the BufferedReader class to obtain input.

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

  • IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can...

    IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can prints the number of words in the string. You can assume that words will only be separated with spaces, commas, and periods. "hello, world" -> 2 "this is cool" -> 3 4) Is Sorted Write a function isSorted() which takes in an list of integers and returns true if the numbers are sorted from smallest to largest.

  • Using the diagram below and the starter code, write Document Processor that will read a file...

    Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...

  • C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string cl...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • a. Write an application for Cody's Car Care Shop that shows a user a list of...

    a. Write an application for Cody's Car Care Shop that shows a user a list of available services: oil change, tire rotation, battery check, or brake inspection. Allow the user to enter a string that corresponds to one of the options, and display the option and its price as $25, $22, $15, or $5, accordingly. Display an error message if the user enters an invalid item. Save the file as CarCareChoice.java. b. It might not be reasonable to expect users...

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

  • PART A Write c program.Please dount use fget Write the statements to read a line of...

    PART A Write c program.Please dount use fget Write the statements to read a line of characters and create a new string by eliminating commas and blanks in the input string. Display the new string as given below: Sample Run: Enter a string: today, tomorrow, next day, will be cold. Next string: todaytomorrownextdaywillbecold. PART B Please dont use fget. Write a complete C program that removes extra spaces between words. Your main function should read the string entered by the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT