Why do i keep getting null?
I'm making a program to test palindromes for my cs class, but i keep getting this:
Enter a phrase to test if it is a palindrome.
mom
The original phrase: "mom" does not match the reversed phrase: "nullmom" The phrase is not a palindrome.
this is my class code:
publicclass PalindromeTester
{
String sentence, reversed;
boolean pal;
publicvoid isPalindrome(String newSentence)
{
sentence = newSentence;
for (int i = sentence.length() -1; i>=0; i--)
reversed += sentence.charAt(i);
if(reversed.equalsIgnoreCase(sentence))
pal =true;
else
pal =false;
}
public String toString()
{
if (pal)
return"The original phrase: \"" +sentence +"\" matches the reversed phrase: \"" +reversed +"\" The phrase is a palindrome.";
else
return"The original phrase: \"" +sentence +"\" does not match the reversed phrase: \"" +reversed +"\" The phrase is not a palindrome.";
}}
This is my client code:
import java.util.*;
publicclass PalindromeTesterClient
{
publicstaticvoid main(String[] args)
{
String sentence;
System.out.println("Enter a phrase to test if it is a palindrome.");
Scanner keyboard =new Scanner(System.in);
sentence = keyboard.nextLine();
PalindromeTester p =new PalindromeTester();
p.isPalindrome(sentence);
System.out.println(p.toString());
}
}

