Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Word Count in Java

  1. #1
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Word Count in Java

    Here is the problem, I am trying to write a small class which helps calculate the number of words in a String entered by the user.

    The trouble is I am using the String method
    split(" ") to calculate the words. But if by any chance there are extra white spaces within the string, it gives a wrong word Count.

    Please advise & suggest

  2. #2
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Word Count in Java

    Here is my code

    public class WordCount {

    public int count;
    public String[] string;

    public int countWords(String s1){

    /*
    * by using method trim, we cut down leading & trailing white space
    * the split method helps break it into individual strings
    * by using the delimiter as " "
    */
    string = (s1.trim()).split(" ");

    count = string.length;//number of individual strings
    return count;

    }//end method countWords

    }//end class WordCount

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: Word Count in Java

    You're spliiting the String on single spaces; as you already have noticed, if words are separated by more than one space, things go wrong; so separate your words on one or more spaces; there's a regular expression for that: " +" (a space followed by a plus sign). But what if words are split by tabs? There's a regular expression for that too: "\\s+", whicch reads 'one or more white space characters.

    kind regards,

    Jos
    DeNiS_M likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Word Count in Java

    Thankyou :-) It worked. I am a month old in Java Coding. Still trying to find my way around.

    Regards
    michaelangelokrk

  5. #5
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: Word Count in Java

    But if by any chance there are extra white spaces within the string, it gives a wrong word Count.
    Could you post your testing cases.
    I ran your code like this:

    Java Code:
    denis@suse113:~/Desktop/Others> java WordCount "Hello how are you"
    Hello
    how
    are
    you
    denis@suse113:~/Desktop/Others> java WordCount "Hello how are you.Nice day"
    Hello
    how
    are
    you.Nice
    day
    It seems it runs fine except the second case but that is expected!

  6. #6
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Word Count in Java

    This is the testing case I was using

    string s1 = "<lots of spaces>Hello<lots of spaces>this<lots of spaces>is<lots of spaces>ME<lots of spaces>"
    Last edited by michaelangelokrk; 09-22-2011 at 09:36 AM.

Similar Threads

  1. Replies: 3
    Last Post: 08-30-2011, 05:44 PM
  2. Replies: 3
    Last Post: 11-09-2010, 01:10 AM
  3. Count same word from many file in directory
    By cassiests in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 10:21 AM
  4. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 06:23 AM
  5. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 08:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •