/*
javac CountCharacters.java
java CountCharacters ToEndingLine.java
File ToEndingLine.java has:
8 lines.
21 words.
155 Characters.
*/
/**ToEndingLine.java:
-------------------------------------------------
public class ToEndingLine {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
*/
import java.util.Scanner;
public class CountCharacters {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("ToEndingLine.java");
Scanner input = new Scanner(file);
int totalLine = 0;
int totalWords = 0;
int totalCharacters = 0;
while (input.hasNext()) {
String s = input.nextLine();
totalLine++;
for (int i = 0; i < s.length(); i++) {
totalCharacters++;
if (Character.isLetter(s.charAt(i)) && (!Character.isLetter(s.charAt(i + 1))))
totalWords++;
}
}
System.out.println("File ToEndingLine.java has:");
System.out.println(totalLine + " lines.");
System.out.println(totalWords + " words.");
System.out.println(totalCharacters + " Characters.");
input.close();
}
}
Introduction to Java Programming编程题9.17<统计文件行、字符、单词数>
最新推荐文章于 2019-08-06 23:27:14 发布