简体   繁体   中英

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. 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.

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.

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.

Thanks to Patashu for the correction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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