-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord Count .java
More file actions
39 lines (33 loc) · 790 Bytes
/
Word Count .java
File metadata and controls
39 lines (33 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
*Input file : https://www.gutenberg.org/files/98/98-0.txt
*Title: A Tale of Two Cities
* A Story of the French Revolution
*
*Author: Charles Dickens
**/
/**
*File Handling
*I/O
*Split string
**/
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.Scanner;
/* Name of the class has to be "Main" only if the class is public. */
public class Wordcount
{
public static void main (String[] args) throws Exception
{
File file = new File("taleoftwocities.txt");//pathname:
Scanner scanner=new Scanner(file);
int words=0;
while(scanner.hasNextLine())
{
String line=scanner.nextLine();
words+=line.split(" ").length;//regex:
}
System.out.println("Words Count in Tale of Two Cities:"+words+".");
}
}