回文检查 - 我做错了什么?

时间:2015-03-12 15:41:40

标签: java swing loops palindrome

我要创建一个程序来检查句子中的回文并显示已找到的回文。我的下面代码一直给我一个"字符串超出范围"错误。我做错了什么?

我的计划:

import javax.swing.JOptionPane;

public class Palindromechkr {
    public static void main(String[] args) {
        //Declare Variables
        String Palin, input, Rinput = "";
        int wordlength, spacePos;
        //Ask for sentance
        input = JOptionPane.showInputDialog("enter a sentance");
        //Split string
        spacePos = input.indexOf(" ");
        String word = input.substring(0, spacePos);
        //Get palindromes   
        System.out.println("Your Palindromes are:");
        for (int counter = 0; counter < input.length(); counter++) {
            //Reverse first word
            wordlength = word.length();
            for (int i = wordlength - 1; i >= 0; i--) {
                Rinput = Rinput + word.charAt(i);
                //Add word to An array of Palindromes 
                if (Rinput.equalsIgnoreCase(word)) {
                    Palin = word;
                    System.out.println("Palin:" + Palin);
                    break;
                }
                //Move on to the next word in the string
                input = input.substring(input.indexOf(" ") + 1) + " ";
                word = input.substring(0, input.indexOf(" "));
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你应该做的是

public static void main(String[] args) {

      String Palin, input, Rinput = "";
      input = new Scanner(System.in).nextLine();
      //Split string
      for(String str : input.split(" ")){
         for (int counter = str.length()-1; counter >= 0; counter--) {
            Rinput = Rinput + str.charAt(counter);
         }
         if (Rinput.equalsIgnoreCase(str)) {
            Palin = str;
            System.out.println("Palin:" + Palin);
         }
         Rinput="";
      }
   }

我不知道你是否知道但是StringBuilder有一个反向的方法。可以这样使用:

public static void main(String[] args) {
      String input;
      input = new Scanner(System.in).nextLine();
      //Split string
      for(String str : input.split(" ")){
         if (new StringBuilder(str).reverse().toString().equalsIgnoreCase(str)) {
            System.out.println("Palin:" + str);
         }
      }
   }

答案 1 :(得分:0)

如果您知道函数,则可以使用递归函数来构建字符串的回文版本(它是递归工作原理的常见示例)。

以下是一个例子:

import javax.swing.JOptionPane;

public class Palindromechkr {
    public static void main(String[] args) {
        //Declare Variables
        String Palin, input, Rinput = "";
        int wordlength, spacePos;
        //Ask for sentance
        input = JOptionPane.showInputDialog("enter a sentance");

        String[] words = input.split(" +"); // see regular expressions to understand the "+"
        for(int i = 0; i < words.length; i++) { // cycle through all the words in the array
            Rinput = makePalindrome(words[i]); // build the palindrome version of the current word using the recursive function

            if(Rinput.equalsIgnoreCase(words[i])) {
                Palin = words[i];
                System.out.println("Palin: " + Palin);
            }
        }
    }

    // this is the recursive function that build the palindrome version of  its parameter "word"
    public static String makePalindrome(String word) {
        if(word.length() <= 1) return word; // recursion base case

        char first = word.charAt(0); // get the first character
        char last = word.charAt(word.length()-1); // get the last character
        String middle = word.substring(1, word.length()-1); // take the "internal" part of the word
                                                            // i.e. the word without the first and last characters

        String palindrome = last + makePalindrome(middle) + first; // recursive call building the palindrome
        return palindrome; // return the palindrome word
    }
}