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

时间:2009-01-29 10:45:55

标签: java count

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

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

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

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

打印完成系统outprintln

我对Java很陌生并且遇到了一些困难。

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

感谢所有

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

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

Hehe这是一项作业,但不是作业作业好的我看得很清楚我可以提供我到目前为止所做的事情,我认为我非常接近,但它不适合我。我有什么遗漏的吗?

// 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();     
    }
}

5 个答案:

答案 0 :(得分:5)

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

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

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

祝你好运!

答案 1 :(得分:2)

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

link看起来是一个不错的起点。

来自here的这个简单示例也可能会给你一些想法:

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

答案 2 :(得分:2)

这是一个解决方案:

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();
    }

}

答案 3 :(得分:0)

不是一个完整的答案,但我建议查看Sun的Java IO教程。它涉及从文件读取和写入。特别是tutorial on Scanners and Formaters

以下是网站

的教程摘要
  

编程I / O经常涉及   整齐地翻译   人类喜欢工作的格式化数据   用。为了帮助你完成这些家务,   Java平台提供了两个API。   扫描仪API将输入分解为   与位相关联的单个令牌   数据的。格式化API组装   数据格式很好,   人类可读的形式。

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

答案 4 :(得分:0)

你可能会利用Apache Commons Utils来获得一些利用,它有一个名为WordUtil的方便工具,用句子和单词做一些简单的事情。

相关问题