Seraching for String highlight all matches
Hi Guys,
This is my first post so hopefully nothing goes wrong.
Right my java language knowledge is not the best in the world and im trying to make a program which reads in a file to JTextArea1 and ouputs everything in the text file to JTextArea1 . I have a little shearch bar and button at the bottom and for instance you put in "cat" and press search it searches for the first exact match for cat and highlights it thats it. What I want the program to do is go through all the text in JTextArea1 and highlight all the exatc matches of "cat".
Heres my code. When the search button is pressed it matches the first exact match and highlights it.
Code :
private void SearchForString(java.awt.event.MouseEvent evt) {
hilit.removeAllHighlights();
String s = jTextField1.getText();
if (s.length() <= 0) {
message.setText("Nothing to search");
return;
}
String content = jTextArea1.getText();
int index = content.indexOf(s, 0);
int end;
if (index >= 0) { // match found
try {
end = index + s.length();
hilit.addHighlight(index, end, painter);
message.setText("'" + s + "' found.");
} catch (BadLocationException e) {
message.setText("Error: " + e.getMessage());
}
}
else {
message.setText("'" + s + "' not found.");
}
}
Any ideas how I can modify my existing code above to highlight all the strings in the JTextArea1 that match the string im searching for.
Thankyou all for your help and if you require additional infomation please let me know.
Re: Seraching for String highlight all matches
Right I have managed to get it to work however it does'nt work the best . The main bit im concerned with is the for loop. Any iodeas how I can optimize this ? The for loop overruns the ammount of searching it needed to do and i get the error message
Code :
private void SearchForString(java.awt.event.MouseEvent evt) {
hilit.removeAllHighlights();
String s = jTextField1.getText();
if (s.length() <= 0) {
message.setText("Nothing to search");
return;
}
String content = jTextArea1.getText();
int index, end;
[B]for(int i=0; i < content.length(); i ++)[/B]
{
index = content.indexOf(s, i);
if (index >= 0) { // match found
try {
end = index + s.length();
hilit.addHighlight(index, end, painter);
// jTextArea1.setCaretPosition(end);
message.setText("'" + s + "' found.");
} catch (BadLocationException e) {
message.setText("Error: " + e.getMessage());
}
}
else {
message.setText("'" + s + "' not found.");
}
}
}
Re: Seraching for String highlight all matches
Quote:
i get the error message
What is the error message? Does it explain what the problem was?
Print out the values of the variables that can be causing the problem to see where they are wrong.
Re: Seraching for String highlight all matches
Quote:
Originally Posted by
Norm
What is the error message? Does it explain what the problem was?
Print out the values of the variables that can be causing the problem to see where they are wrong.
sorry there is no error message but the for loop loops round tooo many times and not effiecient. It uses .Lenght()
java.lang.String
public int length()
Returns the length of this string. The length is equal to the number of Unicode code units in the string.
Returns:
the length of the sequence of characters represented by this object.
so it loops round for every character in theJTextArea1 so after it has found all the matches it will continue to loop and i will get the else bit of code being displayed
Re: Seraching for String highlight all matches
Quote:
after it has found all the matches it will continue to loop
Why loop based on length? Step along the string using the indexOf to find the next match. When indexOf returns NOT found, exit the loop with a break. Read API doc for indexOf to see how to use it to start at past where the last match was found.
Re: Seraching for String highlight all matches
I dont know how to step along the string without using a for loop any ideas?
Re: Seraching for String highlight all matches
The loop should continue until the indexOf method returns not found. Then exit the loop by using a break.
The loop needs to go around enough times for that to happen. A while(true) loop would work.
The next start point for indexOf should be past the last find.
Re: Seraching for String highlight all matches
im sorry im really getting confussed here I have been trying various different aproaches but still nothing heres what i have
Code :
boolean i = true;
while(i == true)
{
index = content.indexOf(s, counter);
if (index >= 0) { // match found
try {
end = index + s.length();
hilit.addHighlight(index, end, painter);
}
catch (BadLocationException e) {
StatusMessage.setText("Error: " + e.getMessage());
}
}
else {
i = false;
StatusMessage.setText("'" + s + "' not found.");
}
++counter;
}
StatusMessage.setText( counter+ "matches found for '" + s + "'");
}
Re: Seraching for String highlight all matches
Please explain what the problem is.
I see that the code should exit the loop when indexOf fails to find the string. Does it do that ok?
Let me repeat what I suggested several times:
The next start point for indexOf should be past the last find.
You are using counter which is NOT related to a position in the string.
Would the value of end be where you want to start the next search from?
Re: Seraching for String highlight all matches
Right sorry for all the confusion I have managed to get it working now apart from 1 minor detail i cant get the counter for the ammount of matches
Code :
private void SearchForString(java.awt.event.MouseEvent evt) {
hilit.removeAllHighlights();
String s = StringSearchTextArea.getText();
if (s.length() <= 0) {
StatusMessage.setText("Nothing to search");
return;
}
String content = MainTextArea.getText();
int index, end, counter=0, matchesfound ;
boolean i = true;
while(i == true)
{
index = content.indexOf(s, counter);
if (index >= 0) { // match found
++counter;
try {
end = index + s.length();
hilit.addHighlight(index, end, painter);
}
catch (BadLocationException e) {
StatusMessage.setText("Error: " + e.getMessage());
}
i = true;
matchesfound = counter;
StatusMessage.setText( matchesfound+ " Matches found for '" + s + "'");
}
else if (index < 0){
i = false;
if(counter == 0)
{
StatusMessage.setText("'" + s + "' not found.");
}
}
}
The program goes through determines if its not there displays the message correctly if it not there. If it can find matches it highlights them however i cant find a way to count all the exact number of matches. I get random numbers all the time
Re: Seraching for String highlight all matches
Try debugging your code by adding print outs of the value of count every time it is changed and every location where indexOf found a match.