java help! how do i read lines in file as paragraph and count words -
i want read lines in file paragraph. example if there lines of text without empty space between them should treat 1 paragraph , want count words.
so far count words correctly doesn't recognise lines of text paragraph. java beginner
scanner file = new scanner(new fileinputstream("/.../output.txt")); int count = 0; while (file.hasnextline()) { string s = file.nextline(); count++; if(s.contains("#avfc")){ system.out.printf("there %d words on line ", s.split("\\s").length-1); system.out.println(count); } } file.close();
if "paragraph" defined "a sequence of non-blank lines", you're doing individual lines, add them until blank line (or end of file), @ point have count completed paragraph. remember reset sum when finish paragraph, ready start of next one. , might need deal multiple blank lines in row.
update: instead of printing out number of words in line, save number variable, wordsinline
. if 0, you're done paragraph (unless count
0, means haven't started paragraph yet) -- print count
, reset it.. if not, part of current paragraph, add count
. finally, once you've finished file, if count
isn't 0, have print count of last paragraph.
Comments
Post a Comment