我正在尝试编写一个将long转换为字符串并检查它是否是回文式的程序,到目前为止,我已经编写了以下内容,但是它给了我一个不兼容的类型错误,而且我似乎找不到什么造成它。 :S
任何帮助将非常感激 :)

该错误发生在第24行,并指出类型不兼容-发现无效,但应为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();
 }
}

最佳答案

当您的函数签名说您想返回一个字符串时,您将无法返回System.out.println。

result = new Character(newAnswer...


newAnswer在该范围内无法解决...您是否正在使用像Eclipse这样的IDE?那可能会对您有很大帮助。

这是执行相同操作的更简单的程序:

public class programPalindrome

{

    static public boolean isPalindromic(long value){
        String valueAsString = Long.toString(value);
        String reverseString = (new StringBuffer(valueAsString)).reverse().toString();
        if(valueAsString.equals(reverseString)){
            return true;
        }
        else{
            return false;
        }
    }


    public static void main(String[] args)
    {

        System.out.println(args[0] + " is palindromic == "
                + isPalindromic(Long.parseLong(args[0])));
    }
}

10-08 06:45