title: How can I create a java program to find the amount of consecutive numbers in an array?
body: <p>I'm trying to make a Java program to find the number of consecutive numbers in an array. For example, if an array has the values, 1,8,10,4,2,3 there are 4 numbers that are consecutive (1,2,3,4). I've created this program, but I'm getting an error on lines 28 and 31 for ArrayIndexOutOfBoundsException, how do I fix the error? (I'm not even sure if the program I made will work if the errors are fixed). Note: I know there are many solutions online for this but I'm a beginner programmer, and I'm trying to do this a more simple way.</p>
  <pre><code>import java.util.Arrays;
  
  class Main {
    public static void main(String[] args) {
      
      consec();
      
  
    }
      
      static void consec()
      {
              
        int[] nums = {16, 4, 5, 200, 6, 7, 70, 8};
        int counter=0;
        
        Arrays.sort(nums);
        for (int i=0; i &lt; nums.length; i++)
          if (i != nums.length - 1)
            System.out.print(nums[i] + &quot;, &quot;);
          else
            System.out.print(nums[i]);
  
        for (int i=0; i &lt; nums.length; i++)
          
          for (int j=i; j &lt; nums.length - i; j++)
            if (nums[j + 1] - 1 == nums[j])
              counter++;
            
              else if (nums[j+1]==counter)
                System.out.print(&quot;Consective amount is&quot; + counter);
              
     
      }  
  }
  </code></pre>