سؤال

I'm trying to write a program which will output what Palindromes will work from entering in a string and how many there are. I keep getting a lot of errors and I'm still trying to get my head around some of the harder topics in Java!

Here's what I have already, as always, all answers are greatly appreciated!

public static boolean Palindrome(String text) {
    int index;
    int palindrome;

    System.out.println("Please enter your text ");
    text = EasyIn.getString();
    for(index = 0; index < amount.length() / 2; index++) {
        if(text.charAt(index) != text.charAt(text.length() - index - 1)) {
            return false;
        }
    }
    System.out.println("The number of valid palindrome(s) is " + amount);
    amount = EasyIn.getString();
}
هل كانت مفيدة؟

المحلول

I think the problem is in amount.length(), you should use text.length(), since you are looping over the half of text. The algorithm works fine. Here is a reduced example:

public static boolean palindrome(String text)
{
    for (int index = 0; index < text.length() / 2; index++) {
        if (text.charAt(index) != text.charAt(text.length() - index - 1)) {
            return false;
        }
    }
    return true;
}

Note:

  • You forgot to add a return true statement, if you don't add one, is possible that the for loop finishes and no return statement is reached, which will cause an error.
  • I would recommend you to follow Java naming conventions. You method should be called like someMethodName instead of SomeMethodName. This last is used for class names.

Edit: As @bobbel commented, you could improve this code by assigning text.length() to a variable and using it inside the for.

نصائح أخرى

There can be two things:

ammount variable that you used either it could be a string array that you are maintaining strings inside it, if this is the case than you have to loop first through array of strings and then maintain one nested loop to check that strings inside it are palindrom or not

or second case is that you have used the variable incorrect it may be text instead of ammount

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.println("enter string to check for palidrome");
    String orginal = in.next();

    int start = 0;
    int middle = orginal.length()/2;
    int end = orginal.length() - 1;
    int i;

    for(i=start; i<=middle; i++) {
        if(orginal.charAt(start) == orginal.charAt(end)) {
            start++;
            end--;
        } else {
            break;
        }
    }

    if(i == middle+1) {
        System.out.println("palidrome");
    } else {
        System.out.println("not palidrome");
    }
} 

This is the Simplest way of Checking Palindrom number.

package testapi;

public class PalindromNumber {

    public static void checkPalindrom(Object number) {
        StringBuilder strNumber = new StringBuilder(number.toString());
        String reverseNumber = strNumber.reverse().toString();
        if (number.toString().equals(reverseNumber)) {
            System.out.println(number + " is palindrom number");
        } else {
            System.out.println(number + " is not palindrom number");
        }
    }

    public static void main(String[] args) {
        checkPalindrom(101);
        checkPalindrom(10.01);
        checkPalindrom("aanaa");
    }

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top