+ 2
How to count the number of sentences in a .txt file in java?
I created a program that will count the number of words that are in a text file in java for a project but when I run it the output says there are "0" sentences in the file. The text file does not contain zero sentences. How do I fix this problem for it show the correct number of sentences in the text file? Link to the code: https://code.sololearn.com/ci4FSx4xImbC/#
3 Respostas
+ 3
BlessingI.123
You can change your if statement
It should be not equal instead of equal.
-> if (!line.equals("")) { }
+ 1
BlessingI.123,
Consider a sentence as a compound of words or acronyms etc. which terminates by a period, followed immediately by a space, or line break.
For the "3 a.m" case, there's no space after "a.", so it shouldn't count for a sentence. AM or PM is more common in time expression these days.
Not a linguistic meself, but I've seen people write acronyms with or without period e.g. 3 AM or 3 A.M, FBI or F.B.I, WHO or W.H.O. It's so common that I begin to think there's no rules in how acronyms are written.
0
Martin Taylor
I changed my if statement to if (!line.equals("")) { } and it returned 6 as the total number of sentences. Then I deleted the sentenceCount on line 17, but it counted 5 sentences. It counted two periods in "3 a.m." that are not a sentence. It should only return 3 sentences.
What do I have to change for it to not include those periods?