0

For some reason, this code throws an exception "Index 20 out of bounds for length 20". I've tried following the code with different System.out.println to try and debug and I figured out as soon as it gets to the last cycle it goes out of bounds. I'm pretty new to java. I'm trying to understand the language and I can't get what I'm doing wrong here.

The point of this problem is to return a string using this sequence of arrays which replaces consecutive numbers with the first and the last one of a subsequence (e.g. in the example below 7,8,9,10,11 would be 7-11).

the array I've used to debug is the following:

 int[] {-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20}.

which should return the string= "-6,-3-1,3-5,7-11,14,15,17-20" .

if(arr[i]+1==arr[i+1]){
      
      while(arr[i+j]+1==arr[i+j+1]){
          end=arr[i+j+1];
          j++;
          
         System.out.println("j="+j);
        }

I've tried printing before this line of code and i=16 which means as soon as it finishes this iteration as j=3 i will finally be 19 and finish the cycle. (since the 27th line "i+=j;" should set i to 19).

Hope I explained the problem as clearly as possible, here is the code I've written so far.

class Solution {
        public static String rangeExtraction(int[] arr) {
      
      String res="";
      //int start=0;
      int end=0;
      int j=0;
     
      
      for(int i=0;i<arr.length;i++){
        
        System.out.println("i="+i);
        
        if(arr[i]+1==arr[i+1]){
          
          while(arr[i+j]+1==arr[i+j+1]){
              end=arr[i+j+1];
              j++;
              
             System.out.println("j="+j);
            }
          
          if(j!=1){ res+=arr[i]+"-"+end+","; }
          else{res+=arr[i]+","+end+",";}
          
          
          i+=j;
          j=0;
          
          }else{
          res+=arr[i]+",";
        }                
      }                
    return res.substring(0,res.length()-1);
  }
}

Jaypal Sodha
  • 2,238
  • 2
  • 10
  • 22
Pogus
  • 17
  • 7
  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Turing85 Jun 02 '21 at 15:26
  • @Turing85 I've found that thread as well but I figured out it probably isn't the case or at least it doesn't seem to me because by following the code I've figured i=19 at the last cycle so by checking the for condition right after it should exit and return res. I've done a bunch of prints here and there and as I've just written index i shouldn't never exceed 19 before the last check. I don't know if I was clear, hope you can help me to understand better my problem! – Pogus Jun 02 '21 at 15:31
  • 2
    `i = 19` and you compare `arr[i]+1` against `arr[i+1]`. What do you think `i+1` is when `i = 19`? – Silvio Mayolo Jun 02 '21 at 15:32
  • Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Jun 02 '21 at 15:35
  • oh okay I see where is the mistake. I thought it would just return false since index=20 in that array is for sure not equal to index 19. I honestly thought it was just an exception regarding the for statement. Okay so I shouldn't compare out of bound values, noted. – Pogus Jun 02 '21 at 15:37
  • sorry for the stupid question, then. I'll keep debugging! Thanks for linking this article @Turing85 I'll check this out asap! – Pogus Jun 02 '21 at 15:39

1 Answers1

0

Your pointer is pointing the index outside the lenght of array that you initilized as int arr. When you are move one index ahead like i+1 you need to run the loop n-1 time or length-1 time there is also i+j in the if condition to overcome this you can use mod "%" operator which will clamp the index inside the lenght of array. You can uncomment the return statement as I ran this program in main function.

 int arr[] = {-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20};

  String res="";
  //int start=0;
  int end=0;
  int j=0;
 
  
  for(int i=0;i<arr.length - 1;i++){
    
    System.out.println("i="+i);
    
    if(arr[i]+1==arr[i+1]){
      
      while(arr[(i+j) % arr.length]+1==arr[(i+j+1) % arr.length]){
          end=arr[i+j+1];
          j++;
          
         System.out.println("j="+j);
        }
      
      if(j!=1){ res+=arr[i]+"-"+end+","; }
      else{res+=arr[i]+","+end+",";}
      
      
      i+=j;
      j=0;
      
      }else{
      res+=arr[i]+",";
    }
    
    
  }
    
    //return res.substring(0,res.length()-1);
}
Syed Mohib Uddin
  • 718
  • 7
  • 16