簡體   English   中英

如何計算Java中每行的單詞數?

[英]how to count the number of words in each line in Java?

我的任務是計算文本文件每行的字數,以空格分隔。 文本文件有 5 行。 我對 Java 很陌生。 到目前為止,我已經有了帶有 B1TextLoader 類和幾種方法的代碼。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.concurrent.ConcurrentHashMap;

public class B1TextLoader {

    ConcurrentHashMap<String, String> documents = new ConcurrentHashMap<String, String>();

    public static void main(String[] args) {

        B1TextLoader loader = new B1TextLoader();
        loader.LoadTextFile("2-3-1BasicTextFile.txt");  
    }

    public void LoadTextFile(String filePath) {

        try {
            System.out.println("Loading file...");
            File f = new File(filePath);
            BufferedReader br = new BufferedReader(new FileReader(f));

            String line = br.readLine();
            Integer counter = 0;
            while (line != null)

            {
                if (line.trim().length() > 0) {
                    documents.put("doc" + counter, line);
                    ;
                    counter++;

                }
                line = br.readLine();
                
            }
            br.close();
        } catch (Exception e) {
            System.out.println("File Load Failed");
        }
        
        System.out.println("Load Complete. Lines loaded: " + documents.size());

    }

    public void CountWordsInDocuments(ConcurrentHashMap<String, String> documents)

    {
        CountWordsInDocuments(documents);

    }
    
    public void CountWordsInDocument(String key, String value)

    {
        String[] words = value.split(" ");
        System.out.println(key + " has " + words.length + " words!");
        documents.forEach(this::CountWordsInDocument);
    }
}

我的輸出是:

Loading file...
Load Complete. Lines loaded: 5

但是,我希望它打印類似

Loading file...
Load Complete. Lines loaded: 5
doc0 has 6 words!
doc1 has 8 words!
doc2 has 4 words!
doc3 has 4 words!
doc4 has 6 words!

我怎樣才能做到這一點?

我的文本文件 2-3-1BasicTextFile.txt 如下所示:

this is a simple text file
you should find that it has five lines
each of varying lengths
and no dirty data
which might make it go wrong

嘗試這個

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * B1TextLoader
 */
public class B1TextLoader {

    public static void main(String[] args) {
        B1TextLoader loader = new B1TextLoader();
        loader.LoadTextFile("./text.txt");
    }

    public void LoadTextFile(String filePath) {
        System.out.println("loading file ...");
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            Object[] lines = br.lines().toArray();
            System.out.println("Load Complete. Lines loaded: " + lines.length);
            for (int i = 0; i < lines.length; i++) {
                System.out.println("doc" + i + " has " + lines[i].toString().split(" ").length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM