I am trying to build a Palindrome finder class that uses an ArrayStack of characters to find palindromes in any word or phrase passed to it. However, when I pass a palindrome as a string to my code, it returns false.
I have debugged my code several times and even wrote the program over, but I cannot figure out why my code isn't working properly. I think it may be something simple that I am overlooking in the isPalindrome() method, the constructor or instance variables.
public class PalindromeFinder {
ArrayStack<Character> charStack = new ArrayStack<>();
String strVerify = "";
private String strCheck;
public PalindromeFinder(String strCheck) {
strCheck = strCheck.toLowerCase();
pushStr(strCheck);
strVerify = popStr();
}
public void pushStr(String strToPush) {
for (int chVal = 0; chVal <strToPush.length(); ++chVal) {
char ch = strToPush.charAt(chVal);
ch = Character.toLowerCase(ch);
if (Character.isAlphabetic(chVal)) {
charStack.push(ch);
}
}
}
public String popStr() {
while (!charStack.empty()) {
strVerify += charStack.pop();
}
return strVerify;
}
public boolean isPalindrome() {
return strVerify == strCheck;
}
}
The following lines of code return false in the main method of my program:
PalindromeFinder p = new PalindromeFinder("mom"); System.out.println(p.isPalindrome());
Any help or improvements on my code would be greatly appreciated.