Member Avatar for mehnihma

Can you help me with this code, I need to find a word, but it only finds first word, how to find multiple words?

lass managerClass implements ActionListener
      {
      
         @Override
         public void actionPerformed(ActionEvent e) {
         
            String myWord = txt.getText();
            Highlighter h = textArea.getHighlighter();
            String searchArea = textArea.getText();
         
            if (e.getSource() == bFind)
            
            {
            
            
               String[] searchArray = null;
               searchArray = searchArea.split("[^A-Z][^a-z][^0-9]");
            
               for(String element : searchArray)
               {
                  if(element.equals(myWord))
                  {
                     pattern = Pattern.compile("\\b"+myWord+"\\b");
                  	
                  	
                  	
                     for (int i = 0; i < searchArray.length; i++) 
                     
                     {
                     
                     
                        Matcher matcher = pattern.matcher(searchArray[i]);
                     
                     
                        while( matcher.find() )
                        
                        
                        {
                           int start = matcher.start();
                           int end = matcher.end();
                        
                           try {
                           
                           
                              //Highlighter hilit = new DefaultHighlighter();
                           
                           
                              Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
                           
                           //h.addHighlight(start, end, DefaultHighlighter.DefaultPainter);
                              h.addHighlight(start, end, painter);
                           	
                              textArea.setSelectionStart(start);
                              textArea.setSelectionEnd(end);
                              textArea.setSelectedTextColor(Color.PINK);
                           
                           } 
                              catch (BadLocationException e1) {
                              
                                 e1.printStackTrace();
                              }
                        }
                     
                     
                     }
                  
                  
                  
                  
                  }
                  else
                  {
                     JOptionPane.showMessageDialog(null, "No match found", "Message", JOptionPane.INFORMATION_MESSAGE);
                  }
               }
            }
         
         
            
            
         }

what exactly do you need? words in a sentence? isn't using the split method easier?

Member Avatar for mehnihma

i need to enter a word and find it word i JTextArea

what do you mean?

so, why do you need all that code above for?

step 1: you enter text in your JTextArea
step 2: you input a word
step 3:
String completeText = jTextArea.getText();
while ( completeText.indexOf(input) != -1){
System.out.println("index of input = " + completeText.indexOf(input));
completeText = resetCompleteText(completeText, input);
// in this method you use indexOf and substring to get the contents of
// completeText after the occurence of input you just detected
}

Member Avatar for mehnihma

but I need to check is there any word that matches the input word and find it, if not show message

I did not understand this

use the indexOf method.
it either returns a positive value, which is the index of that particular word in the complete String, or it returns -1, which means that the word you're looking for does not occur in that complete String.

Member Avatar for mehnihma

I don t get how to do this in this way

Member Avatar for mehnihma

can zou explain me this somehow

what is there to explain, next to what I've explained so far?

String completeText = "This is my Complete Text";
System.out.println("index of not occuring text " + completeText.indexOf("damn"));
System.out.println("index of occuring text " + completeText.indexOf("my"));

you can get the part after 'my' in the completeText by using the substring method
check the String api's for more detailed information

Member Avatar for mehnihma

but I only get first index and nothing after that, what with repeating words?

public class testing
   {
   
   
      public static void main(String[] args) {
      
         String completeText = "This is my Complete my Text";
         String no = "damn";
         String zes = "my";
      
         while ( completeText.indexOf(completeText) != -1){
         
         
         
         
            System.out.println("index of not occuring text " + completeText.indexOf(no));
            System.out.println("index of occuring text " + completeText.indexOf(zes));
         
         
         
         }
      }
   
   
   }
Member Avatar for mehnihma

with my code above, I have the same problem, I can get that index but how to get multiple instances of specific word? and highlight them like in my code above?

in your example my question is again the same as from the frist code

that is why I said: use the substring method.

String original = "this is my original string for my application";
// here, if you search for my, you should get two indexes.
int count = 0;
while ( original.indexOf("my") != -1 ){
  count += 1;
  original = getNewString(original, my);
  // now, create a getNewString method, which takes the original String and the
  // inputString as parameters. yes, you can do it directly with substring without 
  // creating a new method, but it would be better for you if you wrote that part
  // yourself
  // the goal is: after calling this method the first time, original should have
  // as contents : " original string for my application"
  // after the second time:
  // " application"
}
Member Avatar for mehnihma

I will try it, but why matcher is not working properly?

if (e.getSource() == bFind)
            
            {
            
				String myWord = txt.getText();
            Highlighter h = textArea.getHighlighter();
            String searchArea = textArea.getText();
            
               pattern = Pattern.compile("\\b"+myWord+"\\b");
                
            
            
               Matcher matcher = pattern.matcher(myWord);
            
            
            
             
               
               while( matcher.find())
               {
                  int start = matcher.start();
                  int end = matcher.end();
                  
                  try {
                     Font font = new Font("Verdana", Font.BOLD,40);
                     
                     h.addHighlight(start, end, DefaultHighlighter.DefaultPainter);
                  		//String thisS = h.getHighlights();
                  
                  } 
                     catch (BadLocationException e1) {
                        
                        e1.printStackTrace();
                     }
               }
               
             
             
             
                   
            
            
            
            
            }

don't know, wasn't using a matcher.
IMHO, matchers can be great, but if you don't know how to use them properly, they're a lot of hassle and more difficult than it need to be.

Member Avatar for mehnihma

I have done it somehow :D

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.