Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found that the code down below works but it doesn't find the the at the beginning of the sentence

What I have tried:

private class ListenerThe implements ActionListener
  {
     public void actionPerformed(ActionEvent e)
     {
        String str = tf.getText();
        str = str.toLowerCase();
        int count = 0;
        count =0;
       for(int i=1; i<=str.length()-3; i++)
           {

          if(str.substring(i,i+3).equals("the") && !Character.isLetter(str.charAt(i-1)) && !Character.isLetter(str.charAt(i+3)))
                count++;
        }
        answer.setText("There are "+count+" The's in this sentence");
     }
  }
Posted
Updated 29-Apr-19 20:06pm
v2

If you just need the count, you could always use the String.split() function to convert the string into an array; and then use the array.length value.

You would need to be cautions if the first or last words of the string was the word you were searching for.

String (Java Platform SE 7 )[^]
 
Share this answer
 
Comments
Maciej Los 30-Apr-19 1:59am    
5ed!
MadMyche 30-Apr-19 6:48am    
Thank you
Java
for(int i=1; i<=str.length()-3; i++)

Because you start your for loop at index 1, you start analyzing the string at the second character, not the first. Java indices are zero-based.
I think the method indexOf[^] is far more appropriate for your requirement.
 
Share this answer
 
Comments
Maciej Los 30-Apr-19 1:59am    
5ed!
I'd recommend to use Regular Expressions. See: Java Regular Expressions[^]
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {

   private static final String REGEX = "\\bcat\\b";
   private static final String INPUT = "cat cat cat cattie cat";

   public static void main( String args[] ) {
      Pattern p = Pattern.compile(REGEX);
      Matcher m = p.matcher(INPUT);   // get a matcher object
      int count = 0;

      while(m.find()) 
         count++;
      
      System.out.println("Number of matches: "+count);

   }
}
 
Share this answer
 
Comments
phil.o 30-Apr-19 2:19am    
Nice one, my 5!
Maciej Los 30-Apr-19 2:22am    
Thank you.
MadMyche 30-Apr-19 6:49am    
+5
Maciej Los 30-Apr-19 6:55am    
Thank you.
This code fails if word 'the' is first or last word in sentence.

Quote:
I found that the code down below works but it doesn't find the the at the beginning of the sentence

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

For use with debugger, change the code to
Java
for(int i=1; i<=str.length()-3; i++)
{
   String c1 = str.substring(i,i+3);
   String c2 = str.charAt(i-1);
   String c3 = str.charAt(i+3);
   if(str.substring(i,i+3).equals("the") && !Character.isLetter(str.charAt(i-1)) && !Character.isLetter(str.charAt(i+3)))
         count++;
}

to see what are the values used in if while you debug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900