VB icon

Word Count in Java

Email
Submitted on: 7/25/2015 2:40:09 AM
By: Jake R. Pomperada  
Level: Beginner
User Rating: Unrated
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 3695
author picture
 
     A program that I wrote in Java that will ask the user to enter a sentence and then our program will count the number of words in a given sentence. If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at my mobile number 09173084360. Thank you very much and Happy Programming.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Word Count in Java
// Description:A program that I wrote in Java that will ask the user to enter a sentence and then our program will count the number of words in a given sentence.
If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at my mobile number 09173084360.
Thank you very much and Happy Programming.
// By: Jake R. Pomperada
//**************************************

/* word_count.java */
/* Written By: Mr. Jake R. Pomperada, MAED-IT */
/* July 15, 2015 */
/* This program will count the number of words in a given sentence by the user */
import java.util.Scanner;
class word_count {
private static int word_count(String phrase)
{
if (phrase == null)
return 0;
return phrase.trim().split("\\s+").length;
}
public static void main(String args[])
{
 String words;
 char ch;
 do {
 Scanner input = new Scanner(System.in);
 System.out.println();
 System.out.print("\t WORD COUNT PROGRAM ");
 System.out.println("\n");
 System.out.print("Kindly enter a word :=> ");
 words= input.nextLine();
 System.out.println();
 System.out.print("The total number of words is " + word_count(words) +".");
 System.out.println();
 System.out.print("\nDo you want to continue (Type y or n) : ");
 ch = input.next().charAt(0);
 } while (ch == 'Y'|| ch == 'y'); 
 System.out.println();
 System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
 System.out.println("\n\n");
}
}


Other 71 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.