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

Chapter 7 #5, p. 385; Programming Exercises; JAVA PROGRAMMING; 8th Edition; Joyc

ID: 3763627 • Letter: C

Question

Chapter 7 #5, p. 385; Programming Exercises; JAVA PROGRAMMING; 8th Edition; Joyce Farrell. THANK YOU!!!

#5. Write an application that counts the words in a String entered by an user. Words are separated by any combination olf spaces, periods, commas, semicolons, question marks, exclamation points, or dashes. (Figures 7-17 shows two typical excutions in the book on p. 385). Save the file as CountWords.Java

OUTPUT/EXAMPLE

Enter a string: Hello how are you? I am doing fine

There are 8 words in the string

Explanation / Answer

import java.util.*;
class CountWords
{
   public static void main(String args[])
   {
       int c=0;
       Scanner o=new Scanner(System.in);
       System.out.println("Enter String");
       String msg = o.nextLine();

       StringTokenizer st = new StringTokenizer(msg, " :?;.,");
      
       System.out.println("There are "+st.countTokens()+" words in the string");
   }

}