[英]Java - Delimiters ignore punctuation mark at the end of the sentence
因此,我在一个较早的Java线程中问,检查字符串是否是回文。 对于阅读回文字符串不区分大小写 。
我收到了很多很好的反馈,并且做了很多功课(学到了很多东西!),但是由于这是一个实验室作业(学院),所以我无法使用建议的方法(字符串生成器)。 下面的代码(至少是大纲)是我“应该”为该分配编写代码的方式,因此这不是方法的问题。
import java.util.*;
public class Lab04 {
public static void main(String[] args) {
// declaring variables
String sentence;
String word;
// initiating the scanner
Scanner keyboard = new Scanner(System.in);
// prompting the user for a sentence
System.out.println("Please enter the sentence: ");
sentence = keyboard.nextLine();
System.out.println("Your input was: " + '"' + sentence + '"');
// checking if it is a palindrome
sentence = sentence.toLowerCase();
Scanner stringScan = new Scanner(sentence);
**stringScan.useDelimiter("[ \t\n,.-:;'?!\"] + ");**
char leftChar, rightChar;
boolean isPalindrome = true;
while ( stringScan.hasNext() ) {
word = stringScan.next();
leftChar = word.charAt(0);
rightChar = word.charAt(word.length() - 1);
if (!(leftChar == rightChar))
isPalindrome = false;
}
if (isPalindrome)
System.out.println("This is a palindrome.");
else
System.out.println("This is not a palindrome.");
// checking if it is an alliteration
sentence = sentence.toLowerCase();
Scanner stringScan1 = new Scanner(sentence);
**stringScan1.useDelimiter("[ \t\n,.-:;'?!\"]+");**
char firstChar = sentence.charAt(0);
boolean isAlliteration = true;
while ( stringScan1.hasNext() ) {
word = stringScan1.next();
if (firstChar != word.charAt(0) && word.length() > 3)
isAlliteration = false;
}
if (isAlliteration)
System.out.println("This is an alliteration.");
else
System.out.println("This is not an alliteration.");
}
}
我很好奇的部分用粗体写。 我一直在搜寻并尝试使用Java文档,但是我很难找出分隔符的格式是如何工作的。
当前的分隔符可能很杂乱,包含不必要的字符。
我的目标是使分隔符最后忽略标点符号。 它已经可以接近完美了,但是我需要找到一种方法来忽略最后的标点符号。
例:
输入:
Doc, note, I dissent. A fast never prevents a fatness. I diet on cod
输出:
Your input was: "Doc, note, I dissent. A fast never prevents a fatness. I diet on cod"
This is a palindrome.
输入:
Doc, note, I dissent. A fast never prevents a fatness. I diet on cod.
输出:
Your input was: "Doc, note, I dissent. A fast never prevents a fatness. I diet on cod"
This is not a palindrome.
关于其余代码:
我一直在尝试几种不同的方法,所以有一些“额外”代码,例如,我不再需要使用句子.toLowerCase,并且可能只使用一个扫描仪(?),但是我只是想看看是否有针对此问题的修复程序标点问题,因为我觉得剩下的只是我能够弄清楚自己的细节。
提前致谢!
不知道您的代码总体上是否做得正确,但是如果您怀疑要从输入字符串中删除标点符号,那将非常简单。
String lowerCaseInput = input.toLowerCase();
String strippedInput = lowerCaseInput.replaceAll("[^a-z]", "");
转换为小写字母后,replaceAll()会删除所有非小写字母。
感谢Patashu的更正。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.