Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2005, 07:10 PM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Coding for Word Count

Hi,
I am new to this forum and I hope that I will learn more about Java in this web site.

Anyway, I have created a code:


Code:
public class WordCount {
   public static void main(String[] args) {
     //Text string to be analysed
     String text = "Process of planning and implementing"
                  +" the development and distribution of goods"
                  +" to fulfil and produce profits.";
     int words = 0;

     //Analyse all the characters in the string
     int textLength = text.Length();

     for(int i = 0; i < textLength; i++) {
       //Check for words
       if(Character.isWord) {
           words++;
       }
     }
     System.out.println("The Text contained words: " + words + "\n");

     }
  }


Is this correct as to what I want to produce??

Also, where do I have to include, what code, to get CONSOLE WINDOW??

Thanks in Advance.

 
Old February 26th, 2005, 07:34 PM
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to sharpobject
Default

I looked up the String API just for you, and here's what I have:

public class WordCount {
   public static void main(String[] args) {
     String text = "Process of planning and implementing"
                  +" the development and distribution of goods"
                  +" to fulfil and produce profits.";

     String[] words=text.split("\\s");
     int wordCount=words.length;

     System.out.println("The Text contained words: " + wordCount + "\n");
     }
  }

The String.split(String) method returns a String array created by splitting the string around instances of a regular expression. Regular expressions are, quite frankly, difficult. "\s" in a regular expression matches any one whitespace character. Multiple spaces between words will skew results in this implementation, but the simple direction is what I think you were looking for.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Word search - Count Hits bonekrusher XSLT 5 January 2nd, 2008 10:26 AM
how to count lines in word file arshad mahmood VB How-To 2 June 7th, 2006 09:40 PM
character count of ms-word in asp.net sh.rajkumar ASP.NET 1.0 and 1.1 Professional 0 May 19th, 2006 07:24 AM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM
Word Count 12th_Man SQL Language 7 July 25th, 2003 09:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.