Search code examples
javaarraysindexoutofboundsexception

java.lang.ArrayIndexOutOfBoundsException: 4 Error


I'm new to coding and I've been writing this code and trying to make it work but every time I run it it crashes. I've looked things up and will writing this code I've followed java's website on how to properly write down code as well as this site.

Anyways, it would be greatly appreciated if someone can explain to me why this is not working because it seems to me like the logic is there but I don't get why it crashes.

My code:

    import java.util.Scanner;
    import java.lang.String;
    import java.util.*;
    public class Question1
    {
      public static void main(String[] args)
          {
             Scanner keyboard= new Scanner(System.in);
             System.out.println("Enter either letters or numbers and I'll magically tell you if they are consecutive :D");
             String inputedString= keyboard.nextLine();
             boolean consecutiveOrNot=isConsecutive(inputedString);
             System.out.println("Drum rolls...... Is it consecutive: "+ consecutiveOrNot);  //Problem with this line?
          }


      public static boolean isConsecutive(String inputedString)
          {
            //Storing string's units into an array and converting to UpperCase if necessary
            //and storing string's numerical value into the variable 'arrayCharToInt'
              char[] charIntoArray= new char[inputedString.length()];
              int[] arrayCharToInt= new int[inputedString.length()];
              for (int i=0;i<inputedString.length();i++ )
                {
                   charIntoArray[i]=inputedString.charAt(i);
                    if (Character.isLetter(charIntoArray[i]) && Character.isLowerCase(charIntoArray[i]))
                     {
                        charIntoArray[i]= Character.toUpperCase(charIntoArray[i]);
                      }
                    arrayCharToInt[i]=(int) charIntoArray[i];
                }




           // The next if statements and the methods that they call are used to verify 
           //that the content of the initial string is either letters or numbers, but not both together
              boolean[] continuous= new boolean[arrayCharToInt.length];
              boolean[] testContNumbersDecreasing= new boolean[arrayCharToInt.length];
              boolean[] testContNumbersIncreasing= new boolean[arrayCharToInt.length];
              boolean[] testContLettersDecreasing= new boolean[arrayCharToInt.length];
              boolean[] testContLettersIncreasing= new boolean[arrayCharToInt.length];
              Arrays.fill(continuous, true);
               if (lowestValue(arrayCharToInt)>=65 && highestValue(arrayCharToInt)<= 90)
                {
                    for (int x=0;x<arrayCharToInt.length ;x++ ) 
                    {
                       testContLettersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -25));
                       testContLettersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -25));
                    }
                    return (Arrays.equals(continuous,testContLettersIncreasing) || Arrays.equals(continuous,testContLettersDecreasing));
                }

               else if ((lowestValue(arrayCharToInt) >= 48) && (highestValue(arrayCharToInt)<= 57))
                {
                    for (int x=0;x<arrayCharToInt.length ;x++ ) 
                    {
                       testContNumbersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -9));
                       testContNumbersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -9));
                    }
                    return (Arrays.equals(continuous,testContNumbersIncreasing) || Arrays.equals(continuous,testContNumbersDecreasing));

                }
              else
                {
                    return false;
                }

          }



      public static int lowestValue(int[] array)
          {
                int lowest=array[0];
                  for (int counter=0; counter< array.length; counter++)
                    {
                      if( lowest>array[counter])
                            lowest= array[counter];
                    }
                    return lowest;
          }

      public static int highestValue(int[] array)
          {
               int highest=array[0];
                for (int counter=0; counter< array.length; counter++)
                    {
                      if( highest<array[counter])
                        highest= array[counter];
                    }
                return highest;
          }

    }

The main method seems to be fine because it put everything in the isConsecutive method as a comment except for 'return true;' and indeed the program ran and printed true. So I know the problem lies somewhere in the second method.

If there's anything that I did not do right please tell me and that would be greatly appreciated. After all I'm still learning.

Thanks


Solution

  • All of your calls to arrayCharToInt[x+1] are going to go out of bounds on the last iteration of the loop they're in (for example, if arrayCharToInt.length equals 5, the highest that x is going to go is 4. But then x+1 equals 5, which is out of bounds for an array with five cells). You'll need to put in some sort of if( x == arrayCharToInt.length - 1) check.