I've been working on this program for a while now and would very much appreciate a pointer in the right direction.

The program is designed to read from one document and collect data into an array. This seems to work fine.

Then the program is intended to read from a document, and if a key word is in the line, to delete it and insert instead an element from the array.

I keep getting an out of bounds exception at the point in the program where the text is bold / underlined. I ran the program with added code to print the string length and the indexOf value, and it seems to me that it should work.

If anyone can point me in the right direction, I'd really appreciate it.

} // End while loop
fout.close();
fin.close();
} // end main
} // end class

10-07-2011, 01:07 AM

pbrockway2

Re: StringIndexOutOfBoundsException

Use System.out.println() to print the value of sb1 (the string buffer) just before you delete the substring.

(Not exactly part of your problem, but it seems a bit pointless to assign the string contents to str1 when you assign something else to this variable in the next line.)

10-07-2011, 01:07 AM

pbrockway2

Re: StringIndexOutOfBoundsException

Use System.out.println() to print the value of sb1 (the string buffer) just before you delete the substring.

(Not exactly part of your problem, but it seems a bit pointless to assign the string contents to str1 when you assign something else to this variable in the next line.)

10-07-2011, 01:12 AM

pbrockway2

Re: StringIndexOutOfBoundsException

And, please, when you post code use the "code" tags: put [code] at the start of the code and [/code] at the end. This will preserve the formatting when the code appears on a webpage.

10-07-2011, 02:51 AM

Golfnutest

Re: StringIndexOutOfBoundsException

Thanks pbrockway2. Will do.

10-09-2011, 04:22 PM

Golfnutest

Re: StringIndexOutOfBoundsException

I did check the value of the StringBuffer and deleted that line of code. As observed, this wasn't my problem: I added that line in an attempt to fix the problem and forgot to remove it.

Still stuck on this problem if anyone has a suggestion.

10-09-2011, 09:52 PM

pbrockway2

Re: StringIndexOutOfBoundsException

Quote:

I did check the value of the StringBuffer

What was the reported (string) value? And what was the code you used to do that?

10-10-2011, 01:33 AM

Golfnutest

Re: StringIndexOutOfBoundsException

sb1.length() === 0 until I get an OutOfBoundsException, then it jumps to 11, then 12. Please let me know if anything else would be helpful. Thanks for your help.

10-10-2011, 01:42 AM

pbrockway2

Re: StringIndexOutOfBoundsException

Code:

str1 = sb1.delete(pos, 10).toString();

This line of code attempts delete the contents of sb1 from index pos to just before index 10. If things go wrong I can't do much more than to really really suggest you examine the value that sb1 has at that point. Ie insert code to print the string builder out. If you can't understand what you see then posting the output and the code that produced it will help.

10-10-2011, 05:16 AM

Golfnutest

Re: StringIndexOutOfBoundsException

Thanks pbrockway2! I think that is going to be very helpful. It helped me to realize that one of the else if loops had a type o (str instead of str1). Will continue to explore and let you know how it goes.

10-10-2011, 06:01 AM

Golfnutest

Re: StringIndexOutOfBoundsException

Here is where I'm at. I had misunderstood the use of delete(), but for the else if loop using "TARGETNAME", I believe I fixed it. I added some System.out.print lines to see what was going on: the value of pos for the TARGETNAME loop is 56 (line 83). sb1.length() is 15 (line 84).