I've created a program that counts the amt. of characters, words, and lines in a text file...I also need it to count the number of blank lines and the number of vowels in the text document. I don't know why, but I cannot seem to get it to work. If anyone can give me some pointers, I'd very much appreciate it. Thanks much!

Dani AI

Generated

Good start, . Build on the hints from , and with a small, robust reader that is Unicode-aware and does the counting in one pass. Read lines with a BufferedReader, detect blank lines by testing each code point for whitespace, count words with a modest word regex (covers simple hyphen/apostrophe cases), and count vowels with a case-insensitive pattern. The example below shows a practical, easy-to-test implementation.

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.regex.*;

public static void analyze(File file) throws IOException {
    long lines = 0, blank = 0, words = 0, chars = 0, vowels = 0;
    Pattern vowelPat = Pattern.compile("(?i)[aeiou]");
    Pattern wordPat  = Pattern.compile("\\p{L}+(?:['-]\\p{L}+)*");
    try (BufferedReader br = new BufferedReader(
             new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
        String line;
        while ((line = br.readLine()) != null) {
            lines++;
            if (line.codePoints().allMatch(Character::isWhitespace)) blank++;
            chars += line.length(); // readLine removes the line separator
            Matcher wm = wordPat.matcher(line);
            while (wm.find()) words++;
            Matcher vm = vowelPat.matcher(line);
            while (vm.find()) vowels++;
        }
    }
    System.out.printf("lines=%d blank=%d chars=%d words=%d vowels=%d%n",
                      lines, blank, chars, words, vowels);
}

Notes and gotchas: readLine() strips line separators, so character counts here exclude them; if you must include separators, count bytes or add separator lengths carefully (platforms differ). line.codePoints().allMatch(Character::isWhitespace) is more reliable than trim().isEmpty() for many Unicode whitespace characters; if files contain non‑breaking spaces you may want to explicitly treat 0x00A0 as whitespace. The vowel pattern above only matches plain ASCII vowels; to count accented vowels normalize the string (e.g., Normalizer.normalize(line, Form.NFD)) and strip combining marks before matching. Decide up front whether y counts as a vowel; handling contractions and hyphenation depends on the word regex you choose.

Quick debugging tips: verify file encoding (use UTF-8 or the correct charset), test with small crafted files that exercise edge cases (empty lines, lines with only tabs, accented vowels, CRLF vs LF), and print sample lines plus intermediate counts while developing.

Recommended Answers

All 3 Replies

when you read the file, you read it line by line I guess?
What's a special characteristic of a blank line? Hint: it has to do with the number of characters on it after stripping whitespace.

What's a vowel? How can you detect it? When you know that all you need to know is how to loop through your input and count them.

You could have an array of vowels which you could campare with every :( letter and if theres a match increment a vowelCount variable :)

I'm not sure what method you are using to do this program, but a simple way to count the vowels would be to use the String Tokenizer class and set the delimeters to vowels. Use a while structure that checks .hasMoreTokens and loops through until there aren't any left. I recently made a program to do much of the same things, and have some code if you want to see an example. If I have made myself unclear, just drop me a line at
Later
-tom

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.