1

I am having problems with this method.

It is a constructor with 2 parameters: a name and a String with 100 char called in. in must be turned in to a array[10][10] with vak(an object) in it.

I always get the same exception:

StringIndexOutOfBoundsException: String index out of range: 100

This is the method:

 public SpelBord(String naam, String in) {
        int muur=1;
        this.naam = naam;
        System.out.println(in.length());
         for(int i=0;i<in.length();i++) {
            for (int j = 0; j < vakken.length; j++) {
                for (int k = 0; k < vakken[j].length; k++) {

                    if (in.charAt(i) == '.') {
                        this.vakken[j][k] = new Vak();

                    }
                    if (in.charAt(i) == 'K') {
                        this.vakken[j][k] = new Vak(false, false, new   Kist());
                    }
                    if (in.charAt(i) == 'D') {
                        this.vakken[j][k] = new Vak(true, false);

                    }
                    if (in.charAt(i) == 'M') {
                        this.vakken[j][k] = new Vak(false, false, new Man());



                    }
                     if (in.charAt(i) == '#') {
                        this.vakken[j][k] = new Vak(false, true);

                    }
                }
            }
        }

I think it is something with the for loops. thank you in advance!

6
  • 9
    Why are you incrementing i in each if statement ? Commented Apr 13, 2015 at 15:25
  • why do you manually increment i in each if-block? Commented Apr 13, 2015 at 15:25
  • I don't understand why you have the 1st for, it's useless if you have vakken instanciated as new Vak[10][10] Commented Apr 13, 2015 at 15:31
  • 2
    could you give more details as to what this code is suppose achieve? Like what is the variable vakken? and why is` i required to in incremented in each if` statement? Commented Apr 13, 2015 at 15:32
  • Yes that definately is the cause for your error incrementing i inside the dor loop again as others have suggested Commented Apr 13, 2015 at 15:36

3 Answers 3

1

You got this error StringIndexOutOfBoundsException: String index out of range: 100

Because: Suppose you enter a String of 100 character suppose the string is "ABCDEFGHIJK . . . . . . LMNOPQRSTUVWXYZ" in that case in.length=100 Now see what happens to your code: (Read the comments)

for(int i=0;i<in.length();i++) {// i=0
            for (int j = 0; j < vakken.length; j++) {
                for (int k = 0; k < vakken[j].length; k++) {

                    if (in.charAt(i) == '.') {//looking for charAt(0)
                        this.vakken[j][k] = new Vak();
                i++;//if this condition satisfies i=1
                    }
                    if (in.charAt(i) == 'K') {//if last if condition satisfies looking for charAt(1)
                        this.vakken[j][k] = new Vak(false, false, new Kist());
                i++;//if this condition satisfies i=2
                    }
                    if (in.charAt(i) == 'D') {//if last if condition satisfies looking for charAt(2)
                        this.vakken[j][k] = new Vak(true, false);
                i++;//if this condition satisfies i=3
                    }
                    if (in.charAt(i) == 'M') {//if last if condition satisfies looking for charAt(3)
                        this.vakken[j][k] = new Vak(false, false, new Man());

                                 muur++;
                i++;//if this condition satisfies i=4
                    }
                     if (in.charAt(i) == '#') {//if last if condition satisfies looking for charAt(4)
                        this.vakken[j][k] = new Vak(false, true);
            i++;//if this condition satisfies i=5
                    }
                }
            }

Here, in the code the value of your variable i increasing unnecessarily. Now think that i=99 in the loop. Now see what happens in your code:(Read the comments carefully):

    for(int i=0;i<in.length();i++) {// i=99
                for (int j = 0; j < vakken.length; j++) {
                    for (int k = 0; k < vakken[j].length; k++) {

                        if (in.charAt(i) == '.') {//looking for charAt(99)
                            this.vakken[j][k] = new Vak();
                    i++;//if this condition satisfies i=100
                        }
                        if (in.charAt(i) == 'K') {
                        //if last if condition satisfies 
                        //looking for charAt(100) 
                        //now charAt(100) dose not exists 
                        //and an error occurs that String out of range
                            this.vakken[j][k] = new Vak(false, false, new Kist());
                    i++;
                        }
                        if (in.charAt(i) == 'D') {
                            this.vakken[j][k] = new Vak(true, false);
                    i++;
                        }
                        if (in.charAt(i) == 'M') {
                            this.vakken[j][k] = new Vak(false, false, new Man());

                                     muur++;
                    i++;
                        }
                         if (in.charAt(i) == '#') {
                            this.vakken[j][k] = new Vak(false, true);
                i++;
                        }
                    }
                }

I think you understand why the error StringIndexOutOfBoundsException: String index out of range: 100 occurs.

To improve your code you can use else if ladder and do not increment i unnecessarily because i is increment in your for loop - for(int i=0;i<in.length();i++ . And try to setup/clear the logic before starting coding.

Sign up to request clarification or add additional context in comments.

16 Comments

Thank you I understand my mistake. But there is still something not oke. If I set a counter how much time this.vakken[j][k] = new Vak(false, true); it runs. the counter is 3900. I don't know how this can be.. (sorry for my bad sentence construction )
I couldn't understand your comment (If I set a counter how muth time this.vakken[j][k] = new Vak(false, true);).
this.vakken[j][k] = new Vak(false, true); It depends on your input String because there is a check [ if (in.charAt(i) == '#') ] . when the character '#' is found in string it executes.
It's totally depend on your input String.
Sorry :) if i set this (int muur=0;) in the beginning of the method and than I set this ( '(in.charAt(i) == '#') { this.vakken[j][k] = new Vak(false, true); ++muur; } ') (++muur;) Than at the end of the method I wrote this 'System.out.print(muur);' and when I run it muur = 3900
|
1

The for loops takes care of incrementing the value of i,j. You do not need to do it again.

 for(int i=0;i<in.length();i++)

i++ will be executed as the final statement before the loop ends. So will j++ and k++ in its respective loops.

Comments

1

I think that you should have continue statements instead of i++ in the if statements. Then you should not be using the j and k loops. Instead of that use local variables j and k initialised to 0 in the i loop. In the if statements increment the k by 1 and check that k is less than 10. If k is equal to 10 reset k to 0 and increment j by 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.