Ben Gabriëls
Ben Gabriëls

Reputation: 67

nested for loops with String

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!

Upvotes: 1

Views: 351

Answers (3)

Md. Nasir Uddin Bhuiyan
Md. Nasir Uddin Bhuiyan

Reputation: 1596

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.

Upvotes: 1

Blip
Blip

Reputation: 3171

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.

Upvotes: 1

DesirePRG
DesirePRG

Reputation: 6388

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.

Upvotes: 1

Related Questions