2

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

1
  • Is there a stack trace? Commented Oct 6, 2013 at 3:56

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked out thank you, that is what I did plus I added 'continue;' to skip the loop that made the error
Great, glad it worked. And good for you teaching yourself how to do this from tutorials (many people don't have the patience!). Next step, I strongly recommend that you simplify this code. You could actually get the same results with about 10% of the amount of code you have. Just loop over each character of the string, and for each character that's not the first one, set consecutiveOrNot to true if the character is identical to the one before it, and break out of the loop. If this never happens, then you know that there are no consecutives. That would be much more efficient and readable.
0

in the method isConsecutive inside the for loop: for (int x=0;x<arrayCharToInt.length ;x++ ) , you have used arrayCharToInt[x+1]

if the arrayCharToInt lenth is 4 , then you have arrayCharToInt [0] to arrayCharToInt [3].

now consider this statement:arrayCharToInt[x+1] when x is 3 this statement will evalueate to arrayCharToInt[4] resulting in array index out of bounds exception

Comments

0

This error throw when something went wrong in the Array calling function. You got the length and make it print. for eg:

int a[] = {1,2,3,4}

Length of this array is,

int length = a.length

So length = 4 but highest index is 3, not 4. That means index of the array started with 0. So you have to print:

arr[length-1];

In your program,

x == arrayCharToInt.length - 1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.