954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

occurence of a word in a text.

i was trying to write a code for counting the number of ocurence of a particular word from a sentence.it gives me a wrong output it starts checking for the same alphabets in the sentence rather than the word.

import java.lang.String;

   public class StringCount2
 {
   public static void main(String args[])
   {
    String searchFor = "is";
    String base = "This is object oriented programming language  ";
    int len = searchFor.length();
    //System.out.println( "length of object =" +len);
    int result = 0;
    if (len > 0) 
    { 
     int start = base.indexOf(searchFor); 
    // System.out.println("index of object =" + start);
     while (start != -1) 
    {
     result++;
     start = base.indexOf(searchFor, start+len);
     
    }
    }
     System.out.println(result);
    }
 }


can any one help me with this.

newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

If you need to search for a particular word, use the Regex API --

http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html

-- though, be warned. It is not fond of the light-hearted occurrence-searcher.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Condition on incrementing the result must do the trick.
Count should increment only if the word you are searching for in the String(Sentence) does not have any letters of Alphabet before or after it.

Try out this..

public class StringCount2
 {
   public static void main(String args[])
   {
        String searchFor = "is";
        String base = "This is object oriented programming language.is is my favourite.";
        int len = searchFor.length();
        int result = 0;
        if (len > 0) 
        { 
             int start = base.indexOf(searchFor);
             while (start != -1)
             {
                int a=(int)(base.charAt(start-1));//Gives the ASCII equivalent of the character before the search String.
                int b=(int)(base.charAt(start+searchFor.length()));//Gives the ASCII equivalent of the character after the search String.
                //Check if the Character before or after (or both) the search String(*searchFor) is an Alphabet or not.
                if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))
                {
                    //Not to be incremented as string searchFor is a part of a base.
                }
                else
                    result++;//Update the result.
                start = base.indexOf(searchFor, (start+len));
             }
         }
        System.out.println(result);
    }
 }
tyagi
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

thanks tyagi it works.

newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

hi thyagi, i could not understand the if part

if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))

how you got those values for and b
pl. clarify

newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Look at an ASCII value table. You will get your answer.

tuse
Junior Poster
173 posts since Jul 2007
Reputation Points: 32
Solved Threads: 14
 

Yes.The if part is to check whether the ASCII equivalent of the character adjacent the required word in the string on either sides is a letter of the Alphabet or not. Note that it is 'OR' so as to account for non increment in cases like 'this' in the example you have mentioned, which implies that the ASCII equivalent of the character adjacent the required word in the string falls in the range of that of [a-z] or [A-Z] and hence the variable 'result' is not to be incremented.

tyagi
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You