我写了下面的代码来计算不包括空格的字符数,计算字数,计算行数。但是我的代码没有显示正确的输出。
import java.io.*;
class FileCount
{
public static void main(String args[]) throws Exception
{
FileInputStream file=new FileInputStream("sample.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(file));
int i;
int countw=0,countl=0,countc=0;
do
{
i=br.read();
if((char)i==(' '))
countw++;
else if((char)i==('\n'))
countl++;
else
countc++;
}while(i!=-1);
System.out.println("Number of words:"+countw);
System.out.println("Number of lines:"+countl);
System.out.println("Number of characters:"+countc);
}
}
我的文件sample.txt有
hi my name is john
hey whts up
我的出局是
Number of words:6
Number of lines:2
Number of characters:26
答案 0 :(得分:3)
您还需要丢弃其他空格字符,包括重复字符(如果有)。围绕split
的{{1}}为您提供的单词不仅包括所有空格字符,还包含连续出现的任何字符。
获得\\s+
中所有单词的列表后,使用数组和line
的长度方法更容易更新单词和字符的数量。
这样的事情会给你结果:
String
答案 1 :(得分:0)
新行也意味着单词结束。 =>每个单词后都不会有''。
do
{
i=br.read();
if((char)i==(' '))
countw++;
else if((char)i==('\n')){
countl++;
countw++; // new line means also end of word
}
else
countc++;
}while(i!=-1);
文件结尾也应该增加单词的数量(如果'\ n'的''''不是最后一个字符。 此外,处理单词之间的多个空格仍然无法正确处理。
=>您应该考虑更多处理此方法的变化。
答案 2 :(得分:0)
import java.io.*;
class FileCount {
public static void main(String args[]) throws Exception {
FileInputStream file = new FileInputStream("sample.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(file));
int i;
int countw = 0, countl = 0, countc = 0;
do {
i = br.read();
if ((char) i == (' ')) { // You should also check for other delimiters, such as tabs, etc.
countw++;
}
if ((char) i == ('\n')) { // This is for linux Windows should be different
countw++; // Newlines also delimit words
countl++;
} // Removed else. Newlines and spaces are also characters
if (i != -1) {
countc++; // Don't count EOF as character
}
} while (i != -1);
System.out.println("Number of words " + countw);
System.out.println("Number of lines " + countl); // Print lines instead of words
System.out.println("Number of characters " + countc);
}
}
输出:
Number of words 8
Number of lines 2
Number of characters 31
验证
$ wc sample.txt
2 8 31 sample.txt
答案 3 :(得分:0)
试试这个:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileCount {
/**
*
* @param filename
* @return three-dimensional int array. Index 0 is number of lines
* index 1 is number of words, index 2 is number of characters
* (excluding newlines)
*/
public static int[] getStats(String filename) throws IOException {
FileInputStream file = new FileInputStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(file));
int[] stats = new int[3];
String line;
while ((line = br.readLine()) != null) {
stats[0]++;
stats[1] += line.split(" ").length;
stats[2] += line.length();
}
return stats;
}
public static void main(String[] args) {
int[] stats = new int[3];
try {
stats = getStats("sample.txt");
} catch (IOException e) {
System.err.println(e.toString());
}
System.out.println("Number of words:" + stats[1]);
System.out.println("Number of lines:" + stats[0]);
System.out.println("Number of characters:" + stats[2]);
}
}