0

getting this error in my code and not quite understanding how to fix trying to solve coding problems on my own to practice

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6 at StringParenthesis.main(StringParenthesis.java:14)


public class StringParenthesis {

        public static void main (String[]args){
        String word = "recede";
        String[] list,list2 = new String[word.length()];
        
        list = word.toLowerCase().split("");
        String result = "";
            
            for(int i=1;i<list.length;i++) {
                for(int j=1; j<list.length;i++) {
                    
                    if(list[i].equalsIgnoreCase(list[j])){
                        
                        list2[i] = ")";
                        list2[j] = ")";
                    }
                }
            }
            
            for (int k=1; k<list2.length;k++) {
                if(list2[k]!=")") {
                    list2[k]="(";
                    result +=list2[k];
                }else
                    result +=list2[k];
                    
            }
            System.out.println(result);
        }

}



Wietlol
  • 69
  • 1
  • 10
  • It looks like you're assuming the two lists are of the same length in lots of places. One of the lists is allocated of length `word.length()` and the other is set to the result of `split`. There's no reason for them to have matching lengths. – Silvio Mayolo May 15 '21 at 04:03
  • 1
    You are reading outside the bounds of an array. So you are accessing an index greater than the length of the array. Arrays in java are 0-indexed to if an array has size 2, the indicies are 0 and 1. Like the above comment said, you are doing things like list2[i] but not making sure i is within the size limits of list2. – Shaan K May 15 '21 at 04:05
  • Can you provide an input with which you are testing your code? The problem is with the size of `list2` I guess – Rohith V May 15 '21 at 04:14
  • like im just trying to get it to run the word String 'recede' got the question from codewars lol and dedicated too muhc time on it to just quit would adding array.length()-1 or+1 fix it? – Wietlol May 15 '21 at 04:30
  • @Wietlol - If your goal is to learn programming, then my recommendations is that >you< work out if those things would fix it. If you are stuck / lost, I recommend two techniques to for learning how to read and understand your own code ... and what is *actually* does: 1) Hand Execution (see https://commons.swinburne.edu.au/items/da39028b-ee53-46fe-82c9-09c0a573cc95/1/ for example) and Rubber Duck Debugging (see https://rubberduckdebugging.com/ ) These both work by getting you to think more deeply about what code actually does. – Stephen C May 15 '21 at 04:44
  • (On the other hand ... if learning to program is NOT your goal, quitting is a better option than asking a stranger to debug your code for you :-) ) – Stephen C May 15 '21 at 04:46

2 Answers2

2

I don't understand what is the purpose of your code, right now it only fill every list2 character with ")" in an expensive way iterating multiple times for fun, please share your goal to help you fix your method.

Leaving that behid, your code does not work because of:

First: variables "i", "j" and "k" must start with "0" not "1" in order to iterate over the complete array. Right now you are starting from the 2nd letter of "recede", "e" instead of "r".

Second: In the nested for loop where you use "j", you must increment "j", not "i". That is the main reaseon for de Exception.

Third: This not cause any error but it is wrong too. You are not initializin list2 in this line

String[] list, list2 = new String[word.length()];

You are initializing it in this one

list = word.toLowerCase().split("");

If we don't this about the So you can remove from the first assignment. Regardless of the fact that the code doesn't do anything, it could stay this way:

public class StringParenthesis {

  public static void main(String[] args) {
    String word = "recede";

    String[] list, list2 = new String[word.length()];

    list = word.toLowerCase()
               .split("");
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < list.length; i++) {
      for (int j = 0; j < list.length; j++) {

        if (list[i].equalsIgnoreCase(list[j])) {

          list2[i] = ")";
          list2[j] = ")";
        }
      }
    }

    for (int k = 0; k < list2.length; k++) {
      if (list2[k] != ")") {
        list2[k] = "(";
        result.append(list2[k]);
      } else
        result.append(list2[k]);

    }
    System.out.println(result.toString());
  }

}
  • thank you that helped remove the error, I got this exercise from coding wars and the goal is for repeating characters to get ")" while single ones get "(" so for example string "recede" will look like ()()() – Wietlol May 15 '21 at 16:57
1

I don't understand your perpose but index of arrays and collections in java are stared from 0 but size of arrays and collections aren't stared form 0 For example, If I added three element to list, size of list is 3 but that is having 0,1,2 index

public class StringParenthesis {

    public static void main (String[]args){
    
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("orange");
    
        System.out.println(list.size()); // 3
    
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    
    
    }

}
bittap
  • 518
  • 1
  • 6
  • 15