Is the best way to check for 2 words of same type in a string to make a stringtokenizer and story every word in an array then compare it in a for loop?
seems like a hassle for comparing for same words i was wondering if theres a better way?

Recommended Answers

All 6 Replies

String has a split function that's more efficient than StringTokenizer.
But for your problem take a look at regular expressions, they were invented for things like that.
There's a set of functions "indexOf" in String which you can use to look for the starting index of a substring in a string.

you could always use this:

boolean equalsIgnoreCase(String anotherString);

No, that compares two Strings. Here the problem is to detect whether a single word is repeated several times inside a single String.

i have a similar problem where i have to find the number of occurences of a particular word from a sentence, i tried the code but it even counts the words which have those alphabets partly. can anyone help me with this

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);
    }
 }

this thread is two years old, why not post in another topic? many people also frown upon thread hijacking

ok i will do that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.