2

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();     
    }
}
H H
  • 263,252
  • 30
  • 330
  • 514
Evomedia
  • 33
  • 1
  • 6

5 Answers5

5

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!

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
2

Typing "java count words example" into Google came up with a few suggestions.

This link looks to be a decent starting point.

This simple example from here might also give you some ideas:

public class WordCount
{
  public static void main(String args[]) 
  {
    System.out.println(java.util.regex.Pattern.compile("[\\w]+").split(args[0].trim()).length);
  }
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
2

Here's a solution:

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

}
bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • 1
    Well, to start your code doesn't print the top 10 words. The more complicated part as to do with that. – bruno conde Jan 29 '09 at 12:29
  • @bruno conde: Why is there the @Override annotation above the compare method ? It's an anonymous class, it has no parent class that implements copare method... is it a mistake ? – lisak Jan 21 '11 at 20:40
  • @lisak, it's an annotation that acts like a tag. When you look at the code, you immediately know that the compare method is a contract method defined by a superclass or by an interface. – bruno conde Jan 24 '11 at 16:47
  • @bruno conde: yes, I know what it is, but it doesn't belong there. It's wrong, anonymous class doesn't have any superclass hence compare() method cannot override any parent class method – lisak Jan 24 '11 at 16:51
  • I believe that if you try to compile this code, there will be a compilation error – lisak Jan 24 '11 at 16:53
  • @lisak, as of Java 1.6 you can use the @Override annotation to mark a method that implements an interface. See this related post: http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – bruno conde Jan 24 '11 at 18:58
  • @bruno conde: My apologies then :-) – lisak Jan 24 '11 at 19:15
0

Not a complete answer but I'd recomend looking at Sun's Java IO tutorials. It deals with reading and writing from files. Especially the tutorial on Scanners and Formaters

Here is the summary of the tutorial from the website

Programming I/O often involves translating to and from the neatly formatted data humans like to work with. To assist you with these chores, the Java platform provides two APIs. The scanner API breaks input into individual tokens associated with bits of data. The formatting API assembles data into nicely formatted, human-readable form.

So to me it looks like it is exactly the APIs you are asking about

Community
  • 1
  • 1
hhafez
  • 38,949
  • 39
  • 113
  • 143
0

You might get some leverage out of using Apache Commons Utils which has a handy util called WordUtil that does some simple things with sentences and words.

j pimmel
  • 11,617
  • 6
  • 33
  • 43