I’m trying to write a program that converts a long into a string and checks if it is a palindrome, I’ve written the following so far, but it gives me an incompatible types error, and I can’t seem to find what’s causing it. :S
Any help would be much appreciated 🙂
The error occurs at line 24 and it says incompatible types – found void but expected java.lang.String
public class programPalindrome
{
private String go()
{
Input in = new Input ();
System.out.print("Enter Number: ");
return in.nextLine();
long number = in.nextLong();
String Palindrome = Long.toString(number); // converts the long into a string
String newAnswer = reverse(Palindrome);
String anotherAnswer = reverseCheck(Palindrome,newAnswer);
System.out.println("This is a Palindrome" + Palindrome);
}
// Check to see if the two argument Strings are the reverse of each
// other.
private String reverseCheck(String Palindrome, String newAnswer)
{
if (Palindrome.compareTo(newAnswer) == 0) {
return System.out.println("It is a palindrome");
}
else
{
return System.out.println("It is not a palindrome");
}
}
// Return a String which is the reverse of the argument String
private String reverse(final String Palindrome)
{
String result = new String();
int position = 0;
while (position < Palindrome.length())
{
result = new Character(newAnswer.charAt(position)).toString() + result;
position = position + 1;
}
return result;
}
public static void main(String[] args)
{
new programPalindrome().go();
}
}
you can’t return System.out.println when your function signature says you want to return a String… you have some another syntax error on the line
newAnswer can’t be resolved in that scope… are you using an IDE like Eclipse? That would probably help you out a lot.
Here’s a more straightforward program to do the same thing: