943,875 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2768
  • Java RSS
Jul 31st, 2008
0

occurence of a word in a text.

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  1. import java.lang.String;
  2.  
  3. public class StringCount2
  4. {
  5. public static void main(String args[])
  6. {
  7. String searchFor = "is";
  8. String base = "This is object oriented programming language ";
  9. int len = searchFor.length();
  10. //System.out.println( "length of object =" +len);
  11. int result = 0;
  12. if (len > 0)
  13. {
  14. int start = base.indexOf(searchFor);
  15. // System.out.println("index of object =" + start);
  16. while (start != -1)
  17. {
  18. result++;
  19. start = base.indexOf(searchFor, start+len);
  20.  
  21. }
  22. }
  23. System.out.println(result);
  24. }
  25. }

can any one help me with this.
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008
Jul 31st, 2008
0

Re: occurence of a word in a text.

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

http://java.sun.com/javase/6/docs/ap...x/Pattern.html

-- though, be warned. It is not fond of the light-hearted occurrence-searcher.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jul 31st, 2008
0

Re: occurence of a word in a text.

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..

Java Syntax (Toggle Plain Text)
  1. public class StringCount2
  2. {
  3. public static void main(String args[])
  4. {
  5. String searchFor = "is";
  6. String base = "This is object oriented programming language.is is my favourite.";
  7. int len = searchFor.length();
  8. int result = 0;
  9. if (len > 0)
  10. {
  11. int start = base.indexOf(searchFor);
  12. while (start != -1)
  13. {
  14. int a=(int)(base.charAt(start-1));//Gives the ASCII equivalent of the character before the search String.
  15. int b=(int)(base.charAt(start+searchFor.length()));//Gives the ASCII equivalent of the character after the search String.
  16. //Check if the Character before or after (or both) the search String(*searchFor) is an Alphabet or not.
  17. if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))
  18. {
  19. //Not to be incremented as string searchFor is a part of a base.
  20. }
  21. else
  22. result++;//Update the result.
  23. start = base.indexOf(searchFor, (start+len));
  24. }
  25. }
  26. System.out.println(result);
  27. }
  28. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tyagi is offline Offline
3 posts
since Jul 2008
Jul 31st, 2008
0

Re: occurence of a word in a text.

thanks tyagi it works.
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008
Jul 31st, 2008
0

Re: occurence of a word in a text.

hi thyagi, i could not understand the if part

Quote ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008
Aug 1st, 2008
0

Re: occurence of a word in a text.

Look at an ASCII value table. You will get your answer.
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Aug 1st, 2008
0

Re: occurence of a word in a text.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tyagi is offline Offline
3 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Do some manipulations in java?
Next Thread in Java Forum Timeline: Make a 2D Array from ArrayList





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC