1
\$\begingroup\$

I have an assignment to find all occurrences of the word "the" without using regex:

Use the indexOf method to do this. The word "the" could also appear in a larger word, such as "Theresa" or "other".

Please confirm if this is the best way to do that.

import java.util.Scanner;

public class TheFinder
{
    public static void main(String[] args)
    {
        int theCounter;
        int indexOfThe = 0;

        Scanner input = new Scanner(System.in);
        System.out.print("Enter a sentence with a word \"the\" or type \"quit\" to quit: ");
        String sentence = input.nextLine();

        while(!(sentence.equalsIgnoreCase("quit")))
        {
            theCounter = 0;
            sentence = sentence.toLowerCase();

            while(sentence.contains("the"))
            {    
                ++theCounter;
                sentence = sentence.substring((sentence.indexOf("the") + 3), sentence.length());
            }
            if(theCounter > 0)
            {
                System.out.println("The number of times \"the\" appears in the sentence is " + theCounter + ".");
            }
            else 
            {
                System.out.println("There is no word \"the\" in the sentence");
            }

            System.out.print("Please enter a sentence with a word \"the\" or type \"quit\" to quit: ");
            sentence = input.nextLine();
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

Just some ideas:

The code should work as it is and is OK if performance doesn't matter, or the searched strings are small and occurrences few.

  • You don't need to call both contains(...) and indexOf(...); this iterates through the string twice.
  • The substring method copies the whole remaining string into a new string.

Easiest way is to call indexOf(String s, int fromIndex), with the start index parameter.

You can also build a state machine, representing how many letters of "the" have been found sequentially. Then analyze the string char by char.

Also keep in mind that your search will not work for repeating character sequences: if you search for "thethe" instead, and your searched text contains "thethethe", it will be found only once, although it occurs twice. Because your next index skips the entire search phrase when found (here by hardcoded "3"). It depends on what the demands are in your case, and what to do with such search phrases.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for your suggestions. I tested with repeating character sequences. It looks like it is working. Please see my sample output below: $ java TheFinder Enter a sentence with a word "the" or type "quit" to quit: fox jumped thethe wall therice. The number of times "the" appears in the sentence is 3. Please enter a sentence with a word "the" or type "quit" to quit: The Sea world is thethethe place. The number of times "the" appears in the sentence is 4. \$\endgroup\$ – Eric Wessel Nov 26 '17 at 0:00
  • \$\begingroup\$ Searching only "the" is not a problem, only if the beginning of the search phrase repeats within itself (such as "thethe"). It can be simply fixed by not skipping the search phrase length, but continue with the next character. \$\endgroup\$ – Erik Hart Nov 26 '17 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.