Using Scanner to count paragraphs in a text file.
Hello everyone. I have been trying to finish this homework assignment for a while now and I'm running into problems. I have to create a program that reads a simple text file and counts the number of words, sentences, lines, and paragraphs. The program then needs to output the results to another text file.
I have the count for words, lines, and sentences working fine; but paragraphs are really stumping me. According to my professor: "a paragraph is basically a line with words followed by a line with nothing (or, if you wish, the other way around)."
The first thing I thought of was to use scanner and feed the text into a string line by line, then search the string for \n or \r.
Code:
while (paraScan.hasNextLine()){
line = paraScan.nextLine();
if (line.contains("\n") || line.contains("\r")){
paraCount++;
}
}
...
System.out.println("Paragraph count: " + paraCount);
Technically, this would just count the number of lines; I just wanted to see if this code would recoginize \n or \r within the text file before I even attempt to write the actual code for paragraph count. The output is always Paragraph count: 0. This is very wrong, as there is at least 30-40 lines within my text document so I'm doing something wrong.
Does anyone have any idea about how to write this? What it comes down to is I'm not sure about how to: search a line ("fed" into a string?) for a property and then, if said property is met, searchs the next line (string) for another property and if that one is met, increment the paragraph count.
Thanks for any help you can provide.