Java 为什么我的文字计数器有时会差一个?

Java 为什么我的文字计数器有时会差一个?,java,string,file,bufferedreader,filereader,Java,String,File,Bufferedreader,Filereader,大多数情况下,它工作正常。它很少以一为单位计数。猜猜看 public static int countWords(File file) throws FileNotFoundException, IOException{ BufferedReader br = new BufferedReader(new FileReader(file)); String line; List<String> strList = new ArrayLi

大多数情况下,它工作正常。它很少以一为单位计数。猜猜看

public static int countWords(File file) throws FileNotFoundException, IOException{
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        List<String> strList = new ArrayList<>();

        while ((line=br.readLine())!=null){
            String[] strArray= line.split("\\s+");
            for (int i=0; i<strArray.length;i++){
                strList.add(strArray[i]);
            }
        }
        return strList.size();

    }

如果你使用的是<强> java 8 <强>,你可以使用流并过滤你认为是“Word”的东西。例如:

    List<String> l = Files.lines(Paths.get("files/input.txt")) // Read all lines of your input text
            .flatMap(s->Stream.of(s.split("\\s+"))) // Split each line by white spaces
            .filter(s->s.matches("\\w")) // Keep only the "words" (you can change here as you want)
            .collect(Collectors.toList()); // Put the stream in a List
它只是更麻烦


<>希望有帮助。

如果你使用的是<强> java 8 < /St>,你可以使用流并过滤你认为的“Word”。例如:

    List<String> l = Files.lines(Paths.get("files/input.txt")) // Read all lines of your input text
            .flatMap(s->Stream.of(s.split("\\s+"))) // Split each line by white spaces
            .filter(s->s.matches("\\w")) // Keep only the "words" (you can change here as you want)
            .collect(Collectors.toList()); // Put the stream in a List
它只是更麻烦


我希望这会有帮助。

我想第二行分为两个字符串,“,”和“k”。请参阅下面的代码:

import java.util.Arrays;

public static void main(String[] args) {
    String str = "           k";
    String[] array = str.split("\\\s+");
    System.out.println("length of array is " + array.length); // length is 2
    System.out.println(Arrays.toString(array)); //array is [, k]
}

我猜第二行被分成两个字符串,“”和“k”。请参阅下面的代码:

import java.util.Arrays;

public static void main(String[] args) {
    String str = "           k";
    String[] array = str.split("\\\s+");
    System.out.println("length of array is " + array.length); // length is 2
    System.out.println(Arrays.toString(array)); //array is [, k]
}

你认为<代码> \n>代码>是一个词吗?我认为,
k
是你例子中唯一的单词。我猜它是将新行计算为1,制表符计算为2,然后k计算为3;)我怎样才能修好它@BilboBagginsTry创建一个集合/映射或一些其他数据结构来保存所有不想计数的关键字/单词,或者另一种方法是首先从字符串中删除所有不需要的“单词”,然后从中查找单词。特别是在下面的示例中,它给出3而不是2 Ok。这三个要素的价值是什么?是不是
[,\n,k]
\n
前面有空行吗?是<代码> \n[tab ] [新行] [Tab] K</代码>吗?你认为<代码> \n>代码>是一个词吗?我认为,
k
是你例子中唯一的单词。我猜它是将新行计算为1,制表符计算为2,然后k计算为3;)我怎样才能修好它@BilboBagginsTry创建一个集合/映射或一些其他数据结构来保存所有不想计数的关键字/单词,或者另一种方法是首先从字符串中删除所有不需要的“单词”,然后从中查找单词。特别是在下面的示例中,它给出3而不是2 Ok。这三个要素的价值是什么?是不是
[,\n,k]
\n
前面有空行吗?是
\n[tab][newLine][tab]k
?为什么要将流拖到如此简单的问题?文件是一个行流,而行是字流。无需使用
BufferedReader
FileReader
或执行显式循环。结果更短,可读性更高,但您将整个文件读入内存。若文件很大,那个么逐行读取会更好。您的代码将消耗更多内存。为什么要将流拖到如此简单的问题?文件是行流,而行是字流。无需使用
BufferedReader
FileReader
或执行显式循环。结果更短,可读性更高,但您将整个文件读入内存。若文件很大,那个么逐行读取会更好。您的代码将消耗更多内存。