not sure I am having StringIndexOutOfBondException
Hi Fox!
I am trying to write a simle code. In my Code I just copy values from Stringbuffer to String. Then delete everything from Stringbuffer and use String in the next block of code. I am not sure why I am having StringIndexOutOfBondException. What抯 wrong with my String? Can anybody tell me?
Here is my Code
String s1 ="one cat two cats in the yardcat according to the above terms accordingly.";
System.out.println(s1);
Pattern p = Pattern.compile("above",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s1);
StringBuffer sb =new StringBuffer();
System.out.println("This is in our buffer..."+sb.toString());
while (m.find()){
m.appendReplacement(sb,"");
}
m.appendTail(sb);
System.out.println("This is in our buffer NOW ..."+sb.toString());
String test = sb.toString();
System.out.println("In test copy from buffer"+test);
sb.delete(0,sb.length());
System.out.println("This is in our buffer FINALLY ..."+sb.toString());
// works fine till here
s1=test;
System.out.println(s1);
[b]// in this block i get acception.[/b]
Pattern p1 = Pattern.compile("according",Pattern.CASE_INSENSITIVE);
Matcher m1 = p.matcher(s1);
while (m.find()){
m.appendReplacement(sb,"");
}
m.appendTail(sb);
System.out.println("This is in our buffer NOW ..."+sb.toString());
test = sb.toString();
System.out.println("In test copy from buffer"+test);
Thanks in advance
[2090 byte] By [
asyed01a] at [2007-11-27 6:15:17]

this:sb.delete(0,sb.length());should probably be this:sb.delete(0,sb.length() -1);The index starts counting at 0 you know, so the maximum index is length - 1
gimbal2 I used -1 but still same exceptionStringIndexOutOfBoundsException: String index out of range: -5
Hi Guys,
I'm not familiar with Pattern and Matcher, so I thought I would try compiling this and see what I can learn by attempting to fix it. Problem is, it compiled and ran fine for me, no errors....
Maybe I am completely missing something?
"one cat two cats in the yardcat according to the above terms accordingly.
This is in our buffer...
This is in our buffer NOW ...one cat two cats in the yardcat according to the terms accordingly.
In test copy from bufferone cat two cats in the yardcat according to the terms accordingly.
This is in our buffer FINALLY ...
one cat two cats in the yardcat according to the terms accordingly.
This is in our buffer NOW ... terms accordingly.
In test copy from buffer terms accordingly.
"
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class StringError
{
public static void main(String[] agrs)
{
String s1 = "one cat two cats in the yardcat according to the above terms accordingly.";
System.out.println(s1);
Pattern p = Pattern.compile("above",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s1);
StringBuffer sb = new StringBuffer();
System.out.println("This is in our buffer..."+sb.toString());
while (m.find()) {
m.appendReplacement(sb, "");
}
m.appendTail(sb);
System.out.println("This is in our buffer NOW ..."+sb.toString());
String test = sb.toString();
System.out.println("In test copy from buffer"+test);
sb.delete(0,sb.length());
System.out.println("This is in our buffer FINALLY ..."+sb.toString());
// works fine till here
s1=test;
System.out.println(s1);
// in this block i get acception.
Pattern p1 = Pattern.compile("according",Pattern.CASE_INSENSITIVE);
Matcher m1 = p.matcher(s1);
while (m.find()) {
m.appendReplacement(sb, "");
}
m.appendTail(sb);
System.out.println("This is in our buffer NOW ..."+sb.toString());
test = sb.toString();
System.out.println("In test copy from buffer"+test);
}
}
Don't recycle StringBuffers; it makes your code more complicated for no real benefit (and, depending on the Java version you're using, it can cause bugs). Just create a new StringBuffer each time you need one.
But that isn't what's causing your problem. I'm pretty sure the problem is here: Matcher m1 = p.matcher(s1);
while (m.find()) {
You're creating a new Matcher, then using the old one.
Just out of curiosity, why are you doing this the long way, when you could just do this: s1 = s1.replaceAll("(?i)(?:above|according)", "");
uncle_aliceThanks alot for you advice. I didnt know that i can check the words according and accordingly together. So I have a whole list. It mean i can check as many words i want to check together? or is there any limit or 2 or 3 words.
I don't know of any theoretical limit on the number of alternatives you can include in one regex, except that the whole regex has to be less than Integer.MAX_VALUE characters long. But if you have more than a dozen or so items to look for, you don't want to put them in the regex. The regex will tend to get slower as the number of alternatives increases, and it will become increasingly difficult to maintain. In that case, you would be better off using a regex to pluck every word from the string, then look up the word in a Set, List, or other data structure. That way, when you neeed to change the list of target words, you won't have to edit the regex. Set<String> wordList = ...
Pattern p = Pattern.compile("[A-Za-z]+");
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer();
while (m.find())
{
if (wordList.contains(m.group()))
{
m.appendReplacement("");
}
}
m.appendTail(sb);
Hi Fox
One last question. Can anybody tell me how to remove more then one stopping words using the above regex. For instance I have 搊ne cat two cats in the yardcat according to the above terms accordingly.?In my String. Now using regex how I can remove above, according and cat with SPACE character.
I know how to replace cat with any other character or word but HOW can I replace multiple words?
Programming example will be easy for me to understand or if you make modification in my code that will be awesome.
Once again here is my code.
String s1 = "one cat two cats in the yardcat according to the above terms accordingly.";
System.out.println(s1);
Pattern p = Pattern.compile("above",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s1);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "");
}
m.appendTail(sb);
System.out.println("This is in our buffer NOW ..."+sb.toString());
Thanks in advance