0

Nested for loop Problem

public class Loopa{ public static void main(String[] args){ char[] trouble = {'t', 'r', 'o', 'b', 'l', 'e'}; char[] bubble = {'b', 'u', 'b', 'b', 't', 'e'}; int numberOfSameCharacters = 0; for(int i = 0; i < trouble.length; i++){ for(int j = 0; j < bubble.length; i++){ if(trouble[i] == bubble[j]){ numberOfSameCharacters++; } } } } } It's giving me this error Exception in thread "main" java.lang.ArrayIndexOutBoundsException: Index 6 out of bounds for length 6 at Loopa.main(Loopa.java:11)

7th Aug 2019, 2:52 PM
AppInventor
AppInventor - avatar
2 Réponses
+ 2
I think your second loop should be like this for (int j = 0; j < bubble.length; j++) You wrote i++ as the increment instead of j++
7th Aug 2019, 3:14 PM
Agent_I
Agent_I - avatar
+ 1
Can't believe i made this mistake thank you @Agent_l
7th Aug 2019, 3:51 PM
AppInventor
AppInventor - avatar