如何让 java 告诉我文件中正确的行数和字数?
How can I get java to tell me the correct amount of lines and words in a file?
我正在学习如何创建一个程序来读取 class 中的文件。我只能得到 java 来告诉我文件中正确的字数而不是正确的行数。如何让 java 告诉我文件中正确的行数和字数?
public class Reader {
public static void main(String args[]) 抛出 IOException {
BufferedReader demo = new BufferedReader(new FileReader("All.txt"));
Scanner file = new Scanner(demo);
int lineCount = 0;
int wordCount = 0;
//单词计数器
while(file.hasNextLine()) {
String amount = file.nextLine();
String[] txt = amount.split(" ");
for(int i = 0; i < txt.length; i++){
if(txt[i].contains(txt[i]))
wordCount++;
}
}
System.out.println("There are " + wordCount + " words.");
//lineCounter -- 这就是我的问题所在
String line = demo.readLine();
while(line != null){
lineCount++;
line = demo.readLine();
}
System.out.println("There are " + lineCount + " lines.");
}
}
您可以在统计字数的同时统计行数,如下图:
while(file.hasNextLine()) {
lineCount++;// Add this line
String amount = file.nextLine();
String[] txt = amount.split(" ");
wordCount += txt.length;// Add this line
}
System.out.println("There are " + wordCount + " words.");
System.out.println("There are " + lineCount + " lines.");// Add this line
你可以使用这个:
要计算句子,请将每行拆分为 .
、!
、?
。
用space来统计分词就够了。
import java.io.*;
public class File {
public static void main(String[] args) throws IOException {
BufferedReader demo = new BufferedReader(new FileReader("/home/asn/Desktop/All.txt"));
int lineCount = 0;
int wordCount = 0;
String line;
while((line=demo.readLine())!=null) {
String[] words = line.split(" ");
String[] sen = line.split("\.|\!|\?"); // this a a regex expression to split with ., !, and ?
lineCount += sen.length;
wordCount += words.length;
}
System.out.println("There are " + wordCount + " words.");
System.out.println("There are " + lineCount + " lines.");
demo.close();
}
}
我正在学习如何创建一个程序来读取 class 中的文件。我只能得到 java 来告诉我文件中正确的字数而不是正确的行数。如何让 java 告诉我文件中正确的行数和字数?
public class Reader { public static void main(String args[]) 抛出 IOException {
BufferedReader demo = new BufferedReader(new FileReader("All.txt"));
Scanner file = new Scanner(demo);
int lineCount = 0;
int wordCount = 0;
//单词计数器
while(file.hasNextLine()) {
String amount = file.nextLine();
String[] txt = amount.split(" ");
for(int i = 0; i < txt.length; i++){
if(txt[i].contains(txt[i]))
wordCount++;
}
}
System.out.println("There are " + wordCount + " words.");
//lineCounter -- 这就是我的问题所在
String line = demo.readLine();
while(line != null){
lineCount++;
line = demo.readLine();
}
System.out.println("There are " + lineCount + " lines.");
} }
您可以在统计字数的同时统计行数,如下图:
while(file.hasNextLine()) {
lineCount++;// Add this line
String amount = file.nextLine();
String[] txt = amount.split(" ");
wordCount += txt.length;// Add this line
}
System.out.println("There are " + wordCount + " words.");
System.out.println("There are " + lineCount + " lines.");// Add this line
你可以使用这个:
要计算句子,请将每行拆分为 .
、!
、?
。
用space来统计分词就够了。
import java.io.*;
public class File {
public static void main(String[] args) throws IOException {
BufferedReader demo = new BufferedReader(new FileReader("/home/asn/Desktop/All.txt"));
int lineCount = 0;
int wordCount = 0;
String line;
while((line=demo.readLine())!=null) {
String[] words = line.split(" ");
String[] sen = line.split("\.|\!|\?"); // this a a regex expression to split with ., !, and ?
lineCount += sen.length;
wordCount += words.length;
}
System.out.println("There are " + wordCount + " words.");
System.out.println("There are " + lineCount + " lines.");
demo.close();
}
}