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();
 }
 }
}
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Nov 25, 2017 at 21:16
\$\endgroup\$
0

1 Answer 1

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.

answered Nov 25, 2017 at 22:52
\$\endgroup\$
2
  • \$\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\$ Commented Nov 26, 2017 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\$ Commented Nov 26, 2017 at 1:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.