Could someone help java code to read number of lines, words and characrters in paragragh .

Dani AI

Generated

Building on 's question and the quick ideas from and , here is a concise, modern and robust approach that avoids brittle splitting on a single character and correctly handles Unicode. It counts lines, words (using locale-aware boundaries), and characters (counting Unicode code points so surrogate pairs like emoji are not mis-counted).

import java.text.BreakIterator;

public static class Counts {
    public final long lines, words, chars;
    public Counts(long lines, long words, long chars) { this.lines = lines; this.words = words; this.chars = chars; }
}

public static Counts analyzeText(String text) {
    long lines = text.isEmpty() ? 0 : text.lines().count();            // Java 11+; for older Java use text.split("\\R", -1).length
    long chars = text.codePoints().count();                            // counts Unicode code points
    long words = countWords(text);
    return new Counts(lines, words, chars);
}

private static long countWords(String text) {
    BreakIterator itr = BreakIterator.getWordInstance();
    itr.setText(text);
    int start = itr.first(), end;
    long count = 0;
    while ((end = itr.next()) != BreakIterator.DONE) {
        String token = text.substring(start, end);
        if (token.codePoints().anyMatch(Character::isLetterOrDigit)) count++;
        start = end;
    }
    return count;
}

Notes and quick troubleshooting:

  • Newline handling: use String.lines() (Java 11+) or split on "\R" to handle all line separators without manually scanning for "\n" or "\r\n".
  • Word rules: BreakIterator is locale-aware and avoids counting punctuation as words; adjust token checks if hyphenated words or apostrophes must be treated specially.
  • Characters vs grapheme clusters: codePoint count is usually correct for most needs; to count user-perceived characters (grapheme clusters) use BreakIterator.getCharacterInstance(locale).
  • For very large files, stream the input (Files.lines or a Reader) and update counters per line to avoid loading everything into memory.
  • For frequency counts (top words) use a Map and sort entries or a PriorityQueue; prefer library sorting over manual bubble-sort as suggested, and avoid posting uncontextualized external links per .

Recommended Answers

All 4 Replies

Could someone help java code to read number of lines, words and characrters in paragragh .

You can count how may times the \n appears in the paragraph for lines.
For characters the total length of the paragraph String minus the characters: " ", " ", " ", ...
For words user String.split(" "); and count the length of the array minus the elements where you might an empty String with more than one length.

Or you can use regular expressions.

Yes that is a good way to do it. but when you do that you could use StringTokenizer class. it seperates a string to tokens. just change the delimeters to count sentences , lines ,words etc. I hope that could help.

note:
every sencence finish with "."
every line finish with "\n"
every word finish with "\n" , " " , "." or ","

Daniweb member rules include
"Do ensure that all posts contain relevant content and substance and are not simply vehicles for external links"
We also discourage people from simp/ky posting complete solutions.In future please help by pointing people in the right direction.

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.