0

我正在尝试使用一种方法在java中创建一个回文测试器。这就是我到目前为止所拥有的。它是如此接近,我只是无法弄清楚为什么它不会说它是回文并将其反转。

System.out.println("Fun with Palindromes!!");

    Scanner in = new Scanner(System.in);
    System.out.println("Enter the potential palindrome (or enter exit to quit): ");
    String x = in.nextLine();

    while(!x.equals("exit"))
    {
        String t = x.toLowerCase();
        String u = CleanUpString(t);
        Boolean wordCheck = checkPalindrome(u);
        int wordCount = x.length();

        String rev = "";

        for(int i = 0; i <x.length(); i++)
        {
            rev = x.charAt(i)+rev;
        }
        if(wordCheck == true)
        {
            System.out.println("The orginal string\"" + u + "\" contains" + wordCount + "characters." );
            System.out.println("The converted string\"" + rev + "\"is a palindrome");
        }
        else if(wordCheck == false)
        {
            System.out.println("The string \"" + u + "\" contains " + wordCount + " characters");
            System.out.println("\"" + rev + "\" is not a palindrome");
        }
    System.out.println("\nEnter the potential palindrome, or enter exit to quit: ");
    x = in.nextLine();

    }
}   
    public static String CleanUpString(String words)
    {
        words = words.replace(".","");
        words = words.replace("," ,"");
        words = words.replace(":","");
        words = words.replace("!","");
        return words;


    }   



    public static boolean checkPalindrome(String baseball)
    {
        String rev = "";
        for(int i = 0; i<baseball.length()-1; i++)
            {
                rev = baseball.charAt(i) + rev;

            }
        if(rev.equals(baseball))
            return true;
        else
            return false;

    }

}

4

6 回答 6

1

这是我用来确定字符串是否为回文字符串的代码:

private static boolean checkPalindrome(String str){
    if (str == null) 
        return false;
    int len  = str.length();
    for (int i=0;i<len/2 ; i++){
        if (str.charAt(i) != str.charAt(len - i - 1)){
            return false;
            }
    }       
    return true;
}

对于反转字符串,您可以简单地使用:

String reverse = new StringBuffer(string).reverse().toString();

希望这些可以帮助你。

于 2013-10-24T19:23:20.493 回答
1

为此使用StringUtils

import org.apache.commons.lang.StringUtils;

boolean isPalindrome(String word) {
    return StringUtils.reverse(word).equals(word);
}
于 2013-12-24T23:29:55.643 回答
0

这是另一种选择

public class PalindromeTester {
    public static void main(String[] args) {

try {

    String s = args[0];
    int i = args[0].length()-1;
    int i2 = args[0].length();
    char [] chrs = new char[i2];

    for ( int i3 = i; i3 > -1; i3-- ) {
        chrs[i2-i3-1] = (s.charAt(i3) );
        }

        String s2 = String.valueOf(chrs);

        if ( s2.equals(s) ) {
            System.out.println( s + " is a palindrome!");
        } else { 
            System.out.println( s + " is not a palindrome");
        }

    } catch ( ArrayIndexOutOfBoundsException e ) {
        System.out.println("Please enter at least one letter or digit!");
    }

}
    }
于 2015-05-18T23:16:22.833 回答
0

我是这样做的:

public class palindromeTWO 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int right = 0;
        int left = 1;

        System.out.println("Please enter a word: ");
        String word = scan.next();

        int word_length = word.length();

        while(word.charAt(right) == word.charAt(word_length - left) && left < (word_length / 2))
        {
            left++;
            right++;
        }

        if(word.charAt(right) == word.charAt(word_length - left))
        {
            System.out.println("'" + word + "'" + " is a palindrome!");
        }
        else
            {
                System.out.println("'" + word + "'" + " is NOT a palindrome.");
            }
    }
}
于 2015-11-27T14:31:16.693 回答
0

使用递归的第一个实现 -

import java.util.ArrayList;
import java.util.stream.Collectors;

public class PalindromeManager {
private static String str = "ehcache";
private static ArrayList<String> list = new ArrayList<>();

public static void main(String[] args) {
    test(str);

    String output = list.stream().collect(Collectors.joining());

    System.out.println(output);

    if (output.equals(str)) {
        System.out.println("it was palindrome");
    } else {
        System.out.println("Nope! it wasn't");
    }
}

private static void test(String str) {
    if (str.length() <= 0) {
        return;
    }

    String lastChar = "" + str.charAt(str.length() - 1);
    list.add(lastChar);
    test(str.substring(0, str.length() - 1));

  }
}

使用迭代的第二次实现 -

public class PalindromeManager2 {
private static String str = "ehcache";

public static void main(String[] args) {

    int startIndex = 0;
    int lastIndex = str.length() - 1;

    boolean result = true;

    while (true) {

        if (startIndex >= lastIndex) {
            break;
        }

        char first = str.charAt(startIndex);
        char last = str.charAt(lastIndex);

        /*if (first == ' ') {
            startIndex++;
            continue;
        }

        if (last == ' ') {
            lastIndex--;
            continue;
        }*/

        if (first != last) {
            result = false;
            break;
        }

        startIndex++;
        lastIndex--;

    }

    if (result) {
        System.out.println("Yes! It was");
    } else {
        System.out.println("Nope! it wasn't");
    }
  }
}
于 2016-09-08T19:32:53.093 回答
0

在 checkPalindrome 方法中,将 for 循环的条件从 更改i<baseball.length()-1i<baseball.length()

于 2017-07-31T18:17:39.647 回答