2

我已经编写了程序,直到它必须忽略线程中的标点符号和空格,我想知道是否有人可以帮助我编写代码?我一直在尝试的方法似乎不起作用。这是我到目前为止所拥有的:

import java.util.Scanner;

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

{

    String str, another = "y";

    int left, right;

    char charLeft, charRight;


    Scanner scan = new Scanner (System.in);


    while (another.equalsIgnoreCase("y")) // allows y or Y

    {

        System.out.println ("Enter a potential palindrome: ");

        str = scan.nextLine();

        left = 0;

        right = str.length() - 1;


        while (left < right)
        {
            charLeft = str.charAt(left);
            charRight = str.charAt(right);


            if (charLeft == charRight)
            {
                left++;
                right--;
            }

            else if (charLeft == ',' || charLeft == '.' ||
 charLeft == '-' || charLeft == ':' ||
 charLeft == ';' || charLeft == ' ')

                left++;

            else if (charRight == ',' || charRight == '.' ||
 charRight == '-' || charRight == ':' ||
 charRight == ';' || charRight == ' ')
                right--;
            else

                break;

        }

    System.out.println();


        if (left < right)
            System.out.println ("That string is NOT a palindrome.");
                        else

            System.out.println ("That string IS a palindrome.");


        System.out.println();

    System.out.print ("Test another palindrome (y/n)? ");

    another = scan.nextLine();
    }

 }

}
4

7 回答 7

9

只是为了澄清吉姆加里森所说的,你需要的正则表达式如下

String m = "Madam, I'm'',.,.''   Adam";
m = m.toLowerCase().replaceAll("\\W", "");

这将只留下字母和数字,并删除空格和标点符号,即 m 将变为“madamimadam”,您可以对该字符串运行常规回文测试。

您可以在此处了解有关正则表达式的更多信息

于 2009-10-14T03:42:12.000 回答
5

此用于确定单词是否为回文的代码可以简化得多。查找更新的代码

String word;
int z;
int y = 0;
int i = 0;

char letter;

Scanner input = new Scanner(System.in);

System.out.print("Enter a word: ");
word = input.nextLine();

word = word.replaceAll("\\s+", "");
word = word.toLowerCase();

z = word.length()-1;
while (i <= z){

    if ((letter = word.charAt(i)) == (letter = word.charAt(z-i))){
        y += 1;
    }
    i += 1;
}

if (y == (z+1)){
    System.out.println("The word IS a palindrome");
}
else{
    System.out.println("The word is NOT a palindrome");
}

}
}
于 2012-10-19T16:33:27.273 回答
5

这看起来像是一篇很老的帖子,但我想我偶然发现了一个更简单的回文测试解决方案。这将检查第一个和最后一个字符,并在字符不匹配时向内移动并退出程序。

public class CharTest {
    public static void main(String[] args) {
             //converts string to lowercase and replaces everything except numbers
             // and alphabets
        String s = "Niagara. O roar again!".toLowerCase().replaceAll("\\W", "");
        int j=0;
        int k = s.length() - 1;
        while(j < s.length() / 2) { //loops until half the length of the string if 
                                        //even and floor value if odd.
            if (s.charAt(j++) != s.charAt(k--)){//check for first and last chars                                                                                                
                                              //and  go inwards. if char do not match print 'Not a Palindrome' and exit 
                System.out.println("Not a Palindrome");
            System.exit(0);}
        }
        System.out.println("Palindrome");  //if every chars match print "Palindrome"
    }
}
于 2013-08-12T20:40:35.173 回答
4

您可以通过在开始之前删除所有空格和标点符号来显着简化代码。查看 String.replaceAll(regex,replacement)。您将编写一个正则表达式来匹配空格和标点符号,并提供一个空字符串 ("") 作为替换。这将返回一个新字符串,其中包含原始字符串减去您要忽略的字符。

于 2009-10-14T00:12:03.907 回答
3

这是我分配给我的学生的 Java 软件解决方案 (PP3.11) 的编程作业。具有讽刺意味的是,教师解决方案使用Character.isLetterOrDigit(___)(书中从未提及)并使用方法来消除空格和标点符号(在书中甚至没有教过方法),并且 char 不是 AP CS 子集的官方部分. 愚蠢的出版商。

于 2011-11-09T14:23:02.487 回答
2

查看char 的文档条目。具体isLetterOrDigit方法。如果该方法返回 false,则它是标点符号或空格。那里还有其他方法可以帮助解决这个问题。

于 2009-10-14T00:17:38.220 回答
2

您的问题:您没有忽略字母的大小写。因此,如果您尝试 Able was I,在我看到 Elba 之前,它不会正确返回,尽管它是一个真正的回文。

于 2011-03-14T18:36:36.467 回答