简体   繁体   English

Java-分隔符会忽略句子结尾处的标点符号

[英]Java - Delimiters ignore punctuation mark at the end of the sentence

So I asked in an earlier thread Java, Check if a String is a palindrome. 因此,我在一个较早的Java线程中问,检查字符串是否是回文。 Case insensitive about reading strings for palindromes. 对于阅读回文字符串不区分大小写

I received a lot of great feedback and did my homework (learnt a lot!) but since it was a lab assignment (college) I could not use the proposed method (string builder). 我收到了很多很好的反馈,并且做了很多功课(学到了很多东西!),但是由于这是一个实验室作业(学院),所以我无法使用建议的方法(字符串生成器)。 The following code (at least the outline) is how I am 'supposed' to code it for this assignment, so this is not a question about method. 下面的代码(至少是大纲)是我“应该”为该分配编写代码的方式,因此这不是方法的问题。

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.");
}
}

The part I am curious about is written in bold. 我很好奇的部分用粗体写。 I have been googling and trying to use Java docs, but I am having a hard time to finding out how the format of delimiters are working. 我一直在搜寻并尝试使用Java文档,但是我很难找出分隔符的格式是如何工作的。

The current delimiter is probably messy and contains unnecessary characters. 当前的分隔符可能很杂乱,包含不必要的字符。

My goal is to make the delimiter ignore the punctuation mark at the end. 我的目标是使分隔符最后忽略标点符号。 It is already working close to perfect, but I need to find a way to ignore the punctuation mark at the end; 它已经可以接近完美了,但是我需要找到一种方法来忽略最后的标点符号。

Example: 例:

input: 输入:

Doc, note, I dissent. A fast never prevents a fatness. I diet on cod

output: 输出:

Your input was: "Doc, note, I dissent. A fast never prevents a fatness. I diet on cod"
This is a palindrome.

input: 输入:

Doc, note, I dissent. A fast never prevents a fatness. I diet on cod.

output: 输出:

Your input was: "Doc, note, I dissent. A fast never prevents a fatness. I diet on cod"
This is not a palindrome.

Regarding the rest of the code: 关于其余代码:

I have been trying several different things so there's some 'extra' code like I don't need to use the sentence.toLowerCase anymore and could probably just use one scanner (?), but I just wanted to see if there was a fix to the punctuation problem because I feel like the rest are just details I am able to figure out myself. 我一直在尝试几种不同的方法,所以有一些“额外”代码,例如,我不再需要使用句子.toLowerCase,并且可能只使用一个扫描仪(?),但是我只是想看看是否有针对此问题的修复程序标点问题,因为我觉得剩下的只是我能够弄清楚自己的细节。

Thanks in advance! 提前致谢!

Not sure if your code is doing the right thing overall, but if you question is about removing punctuation from the input string, it is pretty straightforward. 不知道您的代码总体上是否做得正确,但是如果您怀疑要从输入字符串中删除标点符号,那将非常简单。

String lowerCaseInput = input.toLowerCase();
String strippedInput = lowerCaseInput.replaceAll("[^a-z]", "");

After converting to lower case, the replaceAll() gets rid of all non-lowercase letters. 转换为小写字母后,replaceAll()会删除所有非小写字母。

Thanks to Patashu for the correction. 感谢Patashu的更正。

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

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