计算行,字,字符和前十个字? [英] Counting lines, words, characters and top ten words?

查看:90
本文介绍了计算行,字,字符和前十个字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望我能做到这一点正确,有人在那里有我需要的答案。 m目前用Eclipse IDE编写一个Java程序,我的问题是这个:



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



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



所有的打印都完成到系统outprintln



我对Java很新,有一些困难。



任何人都可以提供给我这些代码行或知道在哪里我可以找到他们?我想学习代码,这是我最好的学习方式=)



感谢所有



编辑按钮对不起...



我将此添加到我的问题:



不是一个家庭作业确定,我看到我可以提供我到目前为止,我认为im非常接近,但它不工作为我。有没有什么我错过了?

  //类提示


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);
扫描仪扫描=新扫描仪(是);

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列出



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



祝你好运!


Hi I'm pretty new to Stack Overflow so I hope that I'm doing this correctly and that someone out there has the answer I need.

I'm currently coding a program in Java with Eclipse IDE an my question is this:

I need a snippet of code that does the following

It's supposed to get a .TXT file containing text and from that .TXT file count the number of rows and print it, count the number of words and print it, count the number of characters and print it. And finally make a list of the top 10 words used and print that.

Allt the printing is done to system outprintln

I'm pretty new to Java and am having some difficulties.

Anyone out there who can provide me with these lines of code or that knows where i can find them? I want to study the code provided that's how I learn best=)

Thanks to all

Didnt find the edit button sorry...

I Added this to my question:

Hehe it´s an assignment but not a homework assignment ok i see well i could provide what i've done so far, i think im pretty close but it´s not working for me. Is there anything i have missed?

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

解决方案

This sounds a bit too much like a homework assignment to warrant providing a full answer, but I'll give you some tips on where to look in the Java API:

FileReader and BufferedReader for getting the data in. Collections API for storing your data A custom data structure for storing your list of words and occurence count Comparator or Comparable for sorting your data structure to get the top 10 list out

Once you've started work and have something functioning and need specific help, come back here with specific questions and then we'll do our best to help you.

Good luck!

这篇关于计算行,字,字符和前十个字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆