Complete.Org: Mailing Lists: Archives: freeciv-java: October 2000:
[FreeCiv-Java] Some Help Please
Home

[FreeCiv-Java] Some Help Please

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-java@xxxxxxxxxxxx
Subject: [FreeCiv-Java] Some Help Please
From: Houjeh Tiourchi <htiourch@xxxxxxxxxxxxx>
Date: Fri, 06 Oct 2000 14:33:25 -0400

The following code reads a text file, makes a tally of all the different
words used in the file, and prints out a list of the words that appear
most frequently, along with the number of times each word appears.
Please explain to me what each lone of code does and how if possible. I
am a novice and really want to learn this piece of code inside and out.
Thanks.

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

public class TextSearch {
    public static void main (String[ ] args) throws IOException {
        Hashtable Hash = new Hashtable();
        String content = new String("");
        String filename = "test.txt";
        BufferedReader inFile = new BufferedReader(new
FileReader(filename));
        while ((content=inFile.readLine()) != null)
{                          $
            int Ind=0;
            String temp = new String("");
            while(content.length()>Ind+1){
                char thisChar = content.charAt(Ind);
                if ((Character.isLetter(thisChar)) |
|(Character.isDigit(thisChar))) {
                    temp = temp+thisChar;
                    Ind++; //increment variable
                }else   {
                    if(Hash.containsKey(temp)){
                        int y=((Integer)Hash.get(temp)).intValue();
                        Hash.put(temp, new Integer (y+1));
                    }else {
                        Hash.put(temp, new Integer (1));
                    }temp="";
                    Ind++;
                } // end of if statement
            } // end of while loop
        } // end of main while loop

        System.out.println("Word list and frequency in descending
order:\n\n");
        Enumeration c = Hash.keys();
        int size=Hash.size();
        int y=0;
        int[] number = new int[size];
        String[] words = new String[size];
        while (c.hasMoreElements()){
            String s=((String)c.nextElement());
            words[y]=s;
            int v=((Integer)Hash.get(s)).intValue();
            number[y]=v;
            y++;
        } // end of while loop
        for (int j=0; j<=size-1; j++){
            int MaxPos=size-1;
            int max=number[size-1];
            int sort=0; // sort initialized to zero
            while (sort<=size-2){
                if (number[sort]>max){
                    MaxPos=sort;
                    max=number[sort];
                }
                sort++;
            } // end of while loop

            System.out.println("Word: " + words[MaxPos] + "  Used: " +
number[MaxPos] + " time(s)\n");
            number[MaxPos]=-1;
        } // end of for loop
    }
}






[Prev in Thread] Current Thread [Next in Thread]