-2

I keep getting Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 35018 and telling me that 35054 characters is to large even though I just recently checked how many charecters there are and its around 7000 I was wondering if somebody could help me with this. This is the code in question.

         if (twojunglers[0].equals("hecarim")) {
            if (twojunglers[1].equals("leesin")) {
                String codeblock;
                String codeblock1;
                String codeblock2;
                while ((codeblock = br.readLine()) != null) {
                    String makelinesintoone;
                    StringBuilder builder = new StringBuilder();
                    while ((makelinesintoone = br.readLine()) != null){
                        builder.append(makelinesintoone);

                    }
                    int hi = builder.indexOf("Red\">");
                    if (hi >= 0) {
                        codeblock1 = codeblock.substring(0, hi);
                        int cutstring_ = codeblock1.indexOf("Champion\">");
                        if (cutstring_ >= 0) {
                            codeblock2 = codeblock1.substring(0, cutstring_);
                            System.out.println(codeblock2);
Face123
  • 7
  • 4
  • *"I keep getting StringoutofboundsException and telling me that 35054 characters is to large even though I just recently checked how many charecters there are and its around 7000"* That exception is telling you that index 35k is too large for a 7k characters long string and yet you wonder why that this is the case? – Tom May 31 '17 at 19:34
  • that is correct tom. – Face123 May 31 '17 at 19:38
  • Welcome to Stack Overflow. Best to [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). At least post the stacktrace from the exception and point out which line in the posted code it refers to. – Ole V.V. May 31 '17 at 19:44

2 Answers2

0

StringIndexOutOfBoundsException is an exception indicating that an index used on a String object is either negative or greater than the size of the String.

In your actual code, the problem is very probably caused by the substring() invocation below :

int hi = builder.indexOf("Red\">");
if (hi >= 0) {
    codeblock1 = codeblock.substring(0, hi);

You invoke the indexOf() on the builder variable that returns the hi int but then you invoke substring(0, hi) on the codeblock variable.
The problem is that codeblock and builder variables don't refer to the same String as you value them from two distinct read lines :

while ((codeblock = br.readLine()) != null) {

and :

StringBuilder builder = new StringBuilder();
while ((makelinesintoone = br.readLine()) != null){
   builder.append(makelinesintoone);

It makes no sense.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

The problem is very blunt as there is no full explanation or all of your code available. Maybe check what line is giving you the issue and update your question.

Although things to check to maybe save time.

Just make sure that you are assigning strings with strings and not strings with chars or ints, etc.

If you are getting a certain char outside of the bounds of the string using charAt.

Update your question with the full error please.

fluffy
  • 223
  • 1
  • 14