繁体   English   中英

计算线条,单词,字符和前十个单词?

[英]Counting lines, words, characters and top ten words?

嗨,我对Stack Overflow很新,所以我希望我能正确地做到这一点,那里有人有我需要的答案。

我目前用Eclipse IDE编写Java程序,我的问题是:

我需要一段执行以下操作的代码片段

它应该得到一个包含文本的.TXT文件,并从该.TXT文件计算行数并打印它,计算单词数并打印它,计算字符数并打印它。 最后列出前10个单词并打印出来。

Allt打印完成系统outprintln

我是Java的新手,我遇到了一些困难。

在那里谁可以提供这些代码行或知道我在哪里可以找到它们? 我想学习提供的代码,这是我学习最好的方法=)

谢谢大家

没有找到编辑按钮抱歉...

我在我的问题中添加了这个:

呵呵这是一个任务,但不是作业任务好我看得很清楚我可以提供我到目前为止所做的事情,我认为我非常接近,但它不适合我。 有什么我错过了吗?

// Class    Tip


import java.io.*;
import java.util.*;

class Tip
{
    public static void main(String [] args) throws Exception
    {

        String root = System.getProperty("user.dir");   
        InputStream is = new FileInputStream( root + "\\tip.txt" );
        Scanner scan = new Scanner( is );

        String tempString = "";
        int lines = 0;
        int words = 0;
        Vector<Integer> wordLength = new Vector<Integer>();
        int avarageWordLength = 0;

        while(scan.hasNextLine() == true)
        {
                tempString = scan.nextLine();
                lines++;
        }

        is.close();

        is = new FileInputStream( root );
        scan = new Scanner( is );

        while(scan.hasNext() == true)
        {
                tempString = scan.next();
                wordLength.add(tempString.length());
                words++;
        }

        for(Integer i : wordLength)
        {
                avarageWordLength += i;
        }
        avarageWordLength /= wordLength.size();


        System.out.println("Lines : " + lines);
        System.out.println("Words : " + words);
        System.out.println("Words Avarage Length : " + avarageWordLength);

        is.close();     
    }
}

这听起来有点像家庭作业,需要提供完整的答案,但我会给你一些关于在Java API中查看的提示:

用于获取数据的FileReader和BufferedReader。用于存储数据的Collections API用于存储单词列表和出现次数的自定义数据结构Comparator或Comparable用于对数据结构进行排序以获得前10名列表

一旦你开始工作并有一些功能并需要特定帮助,请回到这里提出具体问题,然后我们会尽力帮助你。

祝好运!

Google中输入“java count words examples”提出了一些建议。

这个链接看起来是一个不错的起点。

从这个简单的例子在这里也可以给你一些想法:

public class WordCount
{
  public static void main(String args[]) 
  {
    System.out.println(java.util.regex.Pattern.compile("[\\w]+").split(args[0].trim()).length);
  }
}

这是一个解决方案:

public static void main(String[] args) {
    int nRows = 0;
    int nChars = 0;
    int nWords = 0;

    final HashMap<String, Integer> map = new HashMap<String, Integer>();

    try {
        BufferedReader input = new BufferedReader(new FileReader("c:\\test.txt"));
        try {
            String line = null;
            Pattern p = Pattern.compile("[^\\w]+");
            while ((line = input.readLine()) != null) {
                nChars += line.length();
                nRows++;
                String[] words = p.split(line);
                nWords += words.length;
                for (String w : words) {
                    String word = w.toLowerCase();
                    Integer n = map.get(word);
                    if (null == n)
                        map.put(word, 1);
                    else
                        map.put(word, n.intValue() + 1);
                }
            }
            TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    if (map.get(o1) > map.get(o2))
                        return -1;
                    else if (map.get(o1) < map.get(o2))
                        return 1;
                    else
                        return o1.compareTo(o2);

                }
            });
            treeMap.putAll(map);

            System.out.println("N.º Rows: " + nRows);
            System.out.println("N.º Words: " + nWords);
            System.out.println("N.º Chars: " + nChars);
            System.out.println();
            System.out.println("Top 10 Words:");    
            for (int i = 0; i < 10; i++) {
                Entry<String, Integer> e = treeMap.pollFirstEntry();
                System.out.println("Word: " + e.getKey() + "  Count: " + e.getValue());
            }

        } finally {
            input.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

不是一个完整的答案,但我建议看一下Sun的Java IO教程。 它涉及从文件读取和写入。 特别是关于扫描仪和Formaters教程

以下是该网站的教程摘要

编程I / O通常涉及到人们喜欢使用的整齐格式化数据的转换。 为了帮助您完成这些杂务,Java平台提供了两个API。 扫描器API将输入分解为与数据位相关联的各个令牌。 格式化API将数据组装成格式良好,人类可读的形式。

所以对我来说,它看起来就像你正在询问的API

您可能会利用Apache Commons Utils获得一些利用,它具有一个名为WordUtil的便捷工具,可以用句子和单词完成一些简单的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM