Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Palindrome program problem...

807601Feb 28 2008 — edited Feb 28 2008
I am having a slight problem with this program that I am writing. First off, this program uses 2 classes. One class has the main method in it and the other is the method to actually calculate whether or not a user-inputted string is a Palindrome or not. Also, I am trying to skip over the whitespaces and put this string into an array. If you have any other questions over what I am trying to do or if you need to see the class with the main method in it, repost and I will do the same. Any help is greatly appreciated. Here is the code.
import java.util.*;

public class Palindrome
{
	public static final int MAX_CHARS = 80;

	public boolean checkPalindrome(String text)
	{
		int i;
		int temp = 0;
		int left = 0;
		int right = text.length();
		char aChar;
		char[] tempCharArray = new char[MAX_CHARS];
		boolean result = true;

		for (i = 0; i < MAX_CHARS; i++)//Putting the original String into an array.
		{
			aChar = text.charAt(i);

			tempCharArray[i] = aChar;

            if (Character.isLetter(aChar))//Skipping over the whitespaces and replacing them with the next character in the String.
            {
				tempCharArray[temp] = aChar;
				temp++;
			}

		}

		while (left < right)
		{
			if (tempCharArray[left] != tempCharArray[right])
			{
				return false;
			}
			else
			{
				left++;
				right--;
			}
		}
		return (result);
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2008
Added on Feb 28 2008
23 comments
516 views