public class WordCount {
public static void main(String[] args) {
// TODO Auto-generated method stub
String filename = "/Users/jane/Desktop/file.txt";
Integer line_num=0;
Integer word_num=0;
Integer char_num=0;
try{
FileReader newFile = new FileReader(filename);
BufferedReader br = new BufferedReader(newFile);
String line = br.readLine();
while (line != null) {
line_num++;
char c;
int i=0;
boolean newword = false;
while(i< line.length()){
c = line.charAt(i);
if(c==' ' || c=='\t'){
newword = false;
}else if(c=='\n'){
newword = false;
}else{
if(newword ==false){
newword=true;
word_num++;
}
char_num++;
}
++i;
}
line = br.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("File:" + filename + ": line number = "+ line_num + " word number = "+ word_num
+" char number = "+ char_num );
}
}
No comments:
Post a Comment