Skip to content

Commit cd3d03b

Browse files
Merge pull request #114 from dineshparekh11/CountText
Count_text_word
2 parents 9e78791 + e75ddc0 commit cd3d03b

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

GeeksForGeeks/CountText.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//Counting number of lines, words, characters and paragraphs in a text file using Java
2+
3+
import java.io.*;
4+
5+
public class CountText {
6+
public static void main(String[] args)
7+
throws IOException
8+
{
9+
File file = new File(
10+
"C:\\TextReader.txt"); //Read text from TextReader.txt file
11+
FileInputStream fileInputStream
12+
= new FileInputStream(file);
13+
InputStreamReader inputStreamReader
14+
= new InputStreamReader(fileInputStream);
15+
BufferedReader bufferedReader
16+
= new BufferedReader(inputStreamReader);
17+
18+
/*
19+
* BufferedReader is a class which simplifies reading text from a character input stream.
20+
* It buffers the characters in order to enable efficient reading of text data.
21+
*/
22+
23+
String line;
24+
int WordCount = 0;
25+
int CharacterCount = 0;
26+
int ParagraphCount = 0;
27+
int WhiteSpaceCount = 0;
28+
int SentenceCount = 0;
29+
30+
while ((line = bufferedReader.readLine()) != null) {
31+
if (line.equals("")) {
32+
ParagraphCount += 1;
33+
}
34+
else {
35+
CharacterCount += line.length();
36+
String words[] = line.split("\\s+");
37+
WordCount += words.length; //count words from file
38+
WhiteSpaceCount += WordCount - 1;
39+
String sentence[] = line.split("[!?.:]+"); //count lines from file
40+
SentenceCount += sentence.length;
41+
}
42+
}
43+
if (SentenceCount >= 1) {
44+
ParagraphCount++;
45+
}
46+
System.out.println("Total word count = "
47+
+ WordCount);
48+
System.out.println("Total number of sentences = "
49+
+ SentenceCount);
50+
System.out.println("Total number of characters = "
51+
+ CharacterCount);
52+
System.out.println("Number of paragraphs = "
53+
+ ParagraphCount);
54+
System.out.println("Total number of whitespaces = "
55+
+ WhiteSpaceCount);
56+
}
57+
}

0 commit comments

Comments
 (0)