word count


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: word count

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Posts
    1

    word count

    hey i was working on a word count program and i need a little assistance.i cant use a string tokenizer or a set.after i get a string i need to print out the words and show how many times it was typed...like the following

    "Hello there"

    Hello = 1

    There = 1

    Now i want to know how i can separate the lines by spaces and how can i count them?

    Thanx alot
    Last edited by hjhjhj; 02-12-2006 at 01:16 PM.

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    Once you have the strings of the words that you want, you can simply put them in a HashMap which maps strings to integers. Example code might look like this:
    Code:
    Map<String,Integer> freqTable = new HashMap<String,Integer>();
    String word = ...;
    ...
    Integer i = freqTable.get(word);
    if( i == null ) {
      freqTable.put(word, 1);
    } else {
      freqTable.put(word, i+1); // line requires autoboxing
    }
    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    the countig given by evlich is really good. to split a sentence into words, use the split method from String class:

    Code:
    String text = "Hello there";
    String words[] = text.split("\\s");
    for (int i=0; i<words.length; i++){
      System.out.println("word "+i+"="+words[i]);
    }
    the "\\s" in split stands for any whitespace character, eg space, return and tabs. also see javadocs to class Pattern and String.

Similar Threads

  1. Why can't VB.NET automate Ms Word?
    By Bill Schmidt in forum .NET
    Replies: 1
    Last Post: 02-06-2006, 03:13 PM
  2. need to return total record count, but here is the trick...
    By barbarosa80503 in forum VB Classic
    Replies: 2
    Last Post: 10-28-2005, 03:33 PM
  3. Replies: 5
    Last Post: 02-22-2002, 11:01 AM
  4. Re: word count!!! Faster, Leaner, Meaner!
    By James World in forum .NET
    Replies: 0
    Last Post: 08-13-2001, 04:22 PM
  5. word count!!!
    By Jackee in forum .NET
    Replies: 2
    Last Post: 08-12-2001, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center