开发者

Program to check for a Palindrome

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 progr开发者_StackOverflowamPalindrome

{

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();
 }
}


Try changing

return System.out.println("It is not a palindrome");

to

return "It is not a palindrome";

and

return System.out.println("It is a palindrome");

to

return "It is a palindrome";

(Added - Additionally, there's a logic error in your go() method. It prints out "This is a palindrome" regardless of whether it works or not...)


I made it something like this, below. First, input from the user, then reverse of the string. Next compare user string and reversed one, if is the same -> Print "Palindrom", if not "Not Palindrom". Added "ignoreCase" just in case. ;)

public static void main(String[] args) 
{
    System.out.print("Enter the word: ");
    Scanner userInput = new Scanner(System.in);
    String word = userInput.nextLine();

    String drow = new StringBuilder(word).reverse().toString();

    if(word.equalsIgnoreCase(drow))
    {
        System.out.print("Palindrom");
    }
    else if (!word.equalsIgnoreCase(drow))
    {
        System.out.print("Not Palindrom");
    }
}


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

result = new Character(newAnswer...

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:

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])));
    } 
}


Well, for one thing, you're calling System.out.println() which returns void, and attempting to return that as a String in your reverseCheck() function. Decide: do you want to print the result, or return it (perhaps as a bool)?


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);
}

The compiler shouldn't let you get away with putting unreachable code after the return. I don't think you understand how functions work.


In your reverseCheck() method, you're trying to return a void but you have the return type listed as String. System.out.println() returns void - it just prints to the screen. Rather, return the String "it is a palindrome" or "it is not a palindrome"


return System.out.println("It is not a palindrome");

println does not return anything.


StringBuffer has a reverse method so this should work:

public static boolean checkForPalindrom(String s){
    StringBuffer buf=new StringBuffer(s);
    return s.equals(buf.reverse().toString());
}


This is not explaining what is wrong with your code (it's already explained by others), but why don't you try to use this instead:

public class SO {
    public static void main(String[] args) {
        System.out.println(isPalindrome("aaaaaa"));
        System.out.println(isPalindrome("aaazzaa"));
        System.out.println(isPalindrome("aaazaaa"));
        System.out.println(isPalindrome("zzzbb"));
        System.out.println(isPalindrome("zzbb"));
    }

    public static boolean isPalindrome(String word) {
        for (int i = 0; i <= word.length()/2; i++) {
            if (word.charAt(i) != word.charAt(word.length()-1-i)) {
                return false;
            }
        }

        return true;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜