public static void sentencesChecker(String essay) {

    int count = 0;

    int countcap = 0;

    int i , j;
    for ( i = 0; i < essay.length(); i++) {

        if (essay.charAt(i) == '.') {
            count++;
        }
       for ( char e= 'A'; e<='Z';e++) {
           if (essay.charAt(i)==e){

                countcap++;
           }

        }

    }

    System.out.print("\n\t\t" + countcap + "out of " + count + "sentences start with a capital letter");
}

Dani AI

Generated

The original routine only counts periods and then counts every uppercase character in the whole string, so the two counts are not tied together. As pointed out, that approach breaks on ellipses, question/exclamation endings, abbreviations and Unicode-capital letters. 's hint to test the first character of a sentence is the right idea; the missing piece is a reliable sentence boundary detector and a Unicode-safe way to find the sentence's first letter.

A practical, robust approach is to use java.text.BreakIterator to find sentence boundaries (with a Locale), normalize the text to composed form to handle decomposed accents, then scan each sentence for its first letter code point and test Character.isUpperCase on that code point. The code below implements that pattern and skips fragments that contain no letters (eg. "..." fragments).

public static void countSentenceStarts(String text, java.util.Locale locale) {
    if (text == null || text.isEmpty()) {
        System.out.println("0 out of 0 sentences start with a capital letter");
        return;
    }
    String s = java.text.Normalizer.normalize(text, java.text.Normalizer.Form.NFC);
    java.text.BreakIterator it = java.text.BreakIterator.getSentenceInstance(locale == null ? java.util.Locale.getDefault() : locale);
    it.setText(s);
    int start = it.first();
    int total = 0, caps = 0;
    for (int end = it.next(); end != java.text.BreakIterator.DONE; start = end, end = it.next()) {
        int i = start;
        boolean foundLetter = false;
        while (i < end) {
            int cp = s.codePointAt(i);
            if (Character.isLetter(cp)) {
                foundLetter = true;
                if (Character.isUpperCase(cp)) caps++;
                break;
            }
            i += Character.charCount(cp);
        }
        if (foundLetter) total++;
    }
    System.out.printf("%d out of %d sentences start with a capital letter%n", caps, total);
}

Notes and caveats: choose the Locale that matches the text language; Normalizer helps when letters use combining marks; BreakIterator improves accuracy over simple regex or counting '.' but is not perfect for every abbreviation or dialog boundary. For production-grade segmentation (edge abbreviations, nested quotes), consider an NLP sentence splitter (OpenNLP, ICU4J) and add unit tests that include ellipses, Unicode capitals, question/exclamation endings and sentences with no period as suggested by and .

Recommended Answers

All 3 Replies

I see you failed to test this code, because it doesn't get anywhere near the stated functionality.
If you don't understand why try these test strings:

"This data has one sentence starting with a CAPITAL letter."
"this has none, says James."
"An ellipsis looks like this: ... .
"É is a latin capital letter E acute, Unicode: U+00C9, UTF-8: C3 89."
"What about this one? It has zero periods!"

.., ad that's without getting into what happens if any of the interesting characters are inside a quotation.

Listen: It's good that you are posting and contributing here, but for your own reputation you should be more careful about the quality of what you post.

_Anu_6,
Have you made any progress on testing your sentencesChecker(String essay) {.....} function as suggested by JamesCherrill?
Do you know how to test the your code?

Anu_6 ,
To find a sentences start with a capital letter read a first word of the sentence and check is start with upper case.
Character.isUpperCase(word.charAt(0))
-Anand

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.