PalindromeChecker.java

 
/**
 * @author lisit
 *
 * @param <T>
 */
public class PalindromeChecker<T> {
 
 
    @SuppressWarnings("javadoc")
	private ArrayStack<T> theStack;
    /**
     * @param input
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
 
    /**
     * @param input
     */
    public PalindromeChecker(T[] input) {
    	theStack = new ArrayStack(input.length);
	    	for(int i=0; i < input.length; i++)
	    	{
	    		theStack.push(input[i]);
	    	}
    }
 
    /**
     * @return
     */
    public boolean isPalindrome() {
    	int stacklength = 0;
    	Object[] temp = (Object[]) new Object [50];
	    	for(int i=0; !theStack.isEmpty(); i++){
	    		temp[i] = theStack.pop();
	    		stacklength++;
	    		System.out.println(stacklength);
	    	}
      	boolean tof = false;
	    	for(int j=0; j < stacklength ; j++){
	    		if((j==0)&&(temp[0] == temp[stacklength-1])){
	       				tof = true;
	       		}	
	      		else if(temp[j] == temp[stacklength-j]){	
	    			tof = true;
	    		}
	    	}
    	return tof;	
    }
 
 
}

ArrayStack.java

 
/**
 * @author lisit
 *
 * @param <T>
 */
public class ArrayStack<T> implements StackInterface<T> {
 
	private T[] stack;
	private int topIndex;
	private static final int DEFAULT_INITIAL_CAPACITY = 50;
 
	/**
	 * 
	 */
	public ArrayStack()
	{
		this(DEFAULT_INITIAL_CAPACITY);
	}
 
	/**
	 * @param initialCapcity
	 */
	public ArrayStack(int initialCapcity)
	{
		@SuppressWarnings("unchecked")
		T[] tempStack = (T[]) new Object[initialCapcity];
		stack = tempStack;
		topIndex = -1;
	}
 
    @Override
    public void push(T newEntry) {
    	topIndex++;
    	stack[topIndex] = newEntry;
 
    }
 
    @Override
    public T pop() {
	    topIndex--;
	    return stack[topIndex+1];
    }
 
    @Override
    public T peek() {
    	return stack[topIndex];
    }
 
    @Override
    public boolean isEmpty() {
    	if(stack.length == -1)
    		return true;
    	else
    		return false;
    }
 
    @Override
    public void clear() {
        topIndex = -1;
    }
 
}

PalindromeCheckerTest.java
public class PalindromeCheckerTest {
 
    public static void main(String[] args) {
        int testsPassed = 0;
        int testsAttempted = 0;
 
        PalindromeChecker<Character> pcC1 = new PalindromeChecker<Character>(
                toCharArray("abcd"));
        if (pcC1.isPalindrome()) {
            testsPassed++;
        }
        testsAttempted++;
 
 
        PalindromeChecker<Character> pcC2 = new PalindromeChecker<Character>(
                toCharArray("stack"));
        if (!pcC2.isPalindrome()) {
            testsPassed++;
        }
        testsAttempted++;
 
 
        String[] arr1 = { "I", "love", "CS", "love", "I" };
        PalindromeChecker<String> pcS1 = new PalindromeChecker<String>(arr1);
        if (pcS1.isPalindrome()) {
            testsPassed++;
        }
        testsAttempted++;
 
        // Here you will write some more tests
        // ....
        //
 
        // If you do the extra credit, uncomment these tests, and write
        // some of your own.
        // PalindromeChecker<Character> pcExtra1 = new
        // PalindromeChecker<Character>(
        // sentenceToCharArray("A man, a plan, a canal. Panama."));
        // if (pcExtra1.isPalindrome()) {
        // testsPassed++;
        // }
        // testsAttempted++;
        //
        // PalindromeChecker<Character> pcExtra2 = new
        // PalindromeChecker<Character>(
        // sentenceToCharArray("I Palindrome I"));
        // if (!pcExtra2.isPalindrome()) {
        // testsPassed++;
        // }
        // testsAttempted++;
 
        // If you do the extra extra credit, uncomment these tests, and write
        // some of your own.
        /*PalindromeChecker<Integer> pcExtraExtra1 = new PalindromeChecker<Integer>(
                intToIntArray(1331));
        if (pcExtraExtra1.isPalindrome()) {
            testsPassed++;
        }
        testsAttempted++;
 
        PalindromeChecker<Integer> pcExtraExtra2 = new PalindromeChecker<Integer>(
                intToIntArray(1337));
        if (!pcExtraExtra2.isPalindrome()) {
            testsPassed++;
        }
        testsAttempted++;*/
 
        System.out.println("Congratulations. You passed " + testsPassed
                + " of " + testsAttempted + " tests!");
    }
 
    /**
     * @param s
     *            a String to be converted to an array of Character objects
     * @return the array of Characters created
     */
    private static Character[] toCharArray(String s) {
        Character[] array = new Character[s.length()];
        for (int i = 0; i < s.length(); i++) {
            array[i] = new Character(s.charAt(i));
        }
        return array;
    }
 
    // Extra Credit: Write (and document) a utility function that takes in a
    // single string that is a sentence, and returns an array of Characters
    // (only lowercase,
    // no punctuation or whitespace) that a PalindromeChecker<Character> can
    // take as input. This is so you can test that something like "A man, a
    // plan, a canal. Panama." is a palindrome based on the characters, even
    // though it is not if we look at the words.
 
   // private static Character[] sentenceToCharArray(String sentence) {
   //     return null;
   // }
 
    // Extra Extra Credit: Write (and document) a utility function that takes in
    // an Integer and converts it to an array of Integers that a
    // PalindromeChecker<Integer> can take as input. The array of Integers
    // will be the digits in the base-10 representation of the input. So,
    // for example, intToIntArray(12345) will return [1,2,3,4,5]
 
    //private static Integer[] intToIntArray(Integer input) {
    //    return null;
    //}
}

and this is my error
1
2
3
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
	at ArrayStack.pop(ArrayStack.java:42)
	at PalindromeChecker.isPalindrome(PalindromeChecker.java:35)
	at PalindromeCheckerTest.main(PalindromeCheckerTest.java:9)