简体   繁体   English

Java回文检查器

[英]Java Palindrome checker

I have a issue with my palindrome checker as it is not working properly. 我的回文检查器出现问题,因为它无法正常工作。 It must be able to test these 4 things: 它必须能够测试以下四件事:

    TestPalindrome("Madam, I'm Adam", true);
    TestPalindrome("addbda", false );
    TestPalindrome("", false);
    TestPalindrome("Dammit, I'm mad", true);

This is my code for the palindrome: 这是我的回文代码:

public static boolean IsPalindrome( String inputString )
{
    String reverse = "";
    for(int i = inputString.replaceAll("[^a-zA-Z ]", "").length() -1; i>=0; i--){
        reverse = reverse + inputString.replaceAll("[^a-zA-Z ]", "").charAt(i);
    }
    if(inputString.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase(reverse.toString()) && !inputString.replaceAll("[^a-zA-Z ]", "").isEmpty()){
        return true;
    }

    if(!inputString.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase(reverse.toString()) && !inputString.replaceAll("[^a-zA-Z ]", "").isEmpty()){
        return false;
    }

    if(inputString.replaceAll("[^a-zA-Z ]", "").isEmpty()){
        return false;
    }

    else return true;
         }
}

This is what my output is: 这是我的输出是:

TestPalindrome failed: expected true got false
TestPalindrome passed!
TestPalindrome passed!
TestPalindrome failed: expected true got false

Can anyone help me fix this so I get all passes in the list. 任何人都可以帮助我解决此问题,以便我获得列表中的所有通行证。 I understand similar questions have been asked but I´m not asking how to do a palindrome checker but how to fix my specific one. 我知道有人问过类似的问题,但我不是在问如何做回文检查程序,而是要如何解决我的特定问题。 I used those other questions to learn but for some reason my one doesn't work. 我使用了其他问题来学习,但是由于某种原因我的那个问题不起作用。

In order to such algorithm to work you need to consider to clean the punctual symbols like comma, semmicolon, aposthrofs etc (am talking about ';:' etc) 为了使这种算法起作用,您需要考虑清除诸如逗号,半角分号,撇号等(我在谈论';:'等)的守时符号。

Java has a StringBuilder class and in there is a method to reverse strings Java有一个StringBuilder类,并且有一个反向字符串的方法

so if you clean the String to check, reverse it and compare it again itself you can see if it a palindrome or not... 因此,如果您清洁字符串以进行检查,将其反转并再次进行比较,则可以看到它是否是回文...

Example: 例:

public static void main(String[] args) {
    String word = "Madam, I'm Adam";
    String word2 = "addbda";
    String word3 = "";
    String word4 = "Dammit, I'm mad";
    System.out.println(IsPalindrome(word));
    System.out.println(IsPalindrome(word2));
    System.out.println(IsPalindrome(word3));
    System.out.println(IsPalindrome(word4));
}

private static boolean IsPalindrome(String word) {
    String wordClean = word.replaceAll("[^a-zA-Z  ]", "");
    String reversedWord =  new StringBuilder(wordClean).reverse().toString();
    System.out.println(wordClean);
    System.out.println(reversedWord);
        return reversedWord.equalsIgnoreCase(wordClean)&&!wordClean.isEmpty();
}

this will print 这将打印

MadamImAdam madAmImadaM true MadamImAdam madAmImadaM true

addbda adbdda false addbda adbdda否

false (empty check) 假(空支票)

DammitImmad dammItimmaD true DammitImmad dammItimmaD true

You are not removing spaces, so only strings with spaces in the same places are good. 您不是要删除空格,因此只有空格相同的字符串才是好的。 Just use [^a-zA-Z] against [^a-zA-Z ] 只需使用[^a-zA-Z]反对[^a-zA-Z ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM