GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-May-2008, 14:37
richielit richielit is offline
New Member
 
Join Date: May 2008
Posts: 1
richielit is on a distinguished road

Java Difficulty


Hey Folks,

Ive hit a bit of a stumbling block and i cant seem to get past it. For the last while now i have been trying to count the number of words with the same number of letters in the words from a text file (test.txt). For example an out put would be....

"There are 5 words with 1 letter."
"There are 15 words with 2 letters"
"There are 45 words with 3 letters"

and so on and so forth.I have imagined that it should be done with arrays. One for the number of letters in that word and one for the count of its occurance but so far i have no luck in getting the count to match the letters of the word, but have been able to count all words in the txt file.

Also currently can figure out how to sort an array to out put the 20 most common words.

Both these cases would need to print words to a different txt file (results.txt)

Some of the code that i have tried so far.....................

Any help is greatly welcomed...been banging my head for too long on this now....

Regards,

Richielit


JAVA Code:
 
FileReader fileReader = new FileReader("test.text");
                in = new BufferedReader(fileReader);
                count("presidents.text", in);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
 
 
 
 
public static void main(String[] args) {
        // TODO Auto-generated method stub
        // will store the words read from the file
        List<String> wordList = new ArrayList<String>();
 
        BufferedReader br = null;
        try {
        // attempt to open the words file
        br = new BufferedReader( new FileReader( "test.txt" ) );
 
        String word;
 
        // loop and read a line from the file as long as we dont
        //get null
        while( ( word = br.readLine() ) != null )
        // add the read word to the wordList
        wordList.add( word );
        } catch( IOException e ) {
        e.printStackTrace();
        } finally {
        try {
        // attempt the close the file
        br.close();
        } catch( IOException ex ) {
        ex.printStackTrace();
        }
        }
 
        // initialize a new string array equal to the size of the
        //wordList
        String[] words = new String[ wordList.size() ];
Last edited by admin II : 07-May-2008 at 04:16. Reason: Please surround your Java code with [java] your code [/java]
  #2  
Old 06-May-2008, 15:12
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 451
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Java Difficulty


Once you have read all of your words in to your list, you could create a map.

JAVA Code:
// < key = WordLength, value = NumberOfWordsOfThisLength >
Map<Integer,Integer> M = new HashMap<Integer,Integer>();
for(int i=0;i<words.length;++i)
{
  Integer temp = new Integer(words[i].length());
  Integer freq = M.get(temp);
  M.put(temp,(freq == null)? 1 : freq+1);
}

See this tutorial for more info.
  #3  
Old 07-May-2008, 20:59
JustinFox JustinFox is offline
Junior Member
 
Join Date: Mar 2008
Posts: 59
JustinFox will become famous soon enough

Re: Java Difficulty


I would make a linked list, and insert the words as nodes based on their length. That way you could just take the top 20 words that have the most inserts.

and also you could have more than just the number of characters in the node if you wanted more information stored..

Justin Fox
  #4  
Old 07-May-2008, 21:26
JustinFox JustinFox is offline
Junior Member
 
Join Date: Mar 2008
Posts: 59
JustinFox will become famous soon enough

Re: Java Difficulty


Here is a simple example of a DLL that I wrote up, suprisingly it worked perfect lol.

JAVA Code:

public class DoubleLL{

Node head = null;
Node curr = null;

public class Node{

Node next;
Node prev;

private int order;
private String name;
private int numChars;

public Node()
{next = null; prev = null;}

}

public DoubleLL(){

head = new Node();

}

public void addNode()
{

if(head.next == null)
{
Node newN = new Node();
head.next = newN;
curr = head.next;
head.prev = newN;
}
else
{

Node newN = new Node();
curr.next = newN;
newN.next = head;

}
}

public int getCount()
{

Node front = head;
front = front.next;
int i = 1;

while(front != head)
{

front = front.next;
i ++;

}

return i;

}

public static void main(String [] args)
{

DoubleLL mylist = new DoubleLL();
mylist.addNode();
mylist.addNode();
mylist.addNode();

System.out.println(mylist.getCount());

}

}



you could always write a method that will insert after a number, or even better, insert before a node which number of characters is greater than the one to insert, and if you reach then end, then let head.prev.next = newnode and newnode.next = head

hope this atleast sounds interesting,

Justin Fox
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C code to Java bengt23648 Java Forum 2 09-Sep-2007 05:33
What is the equivalent in Java for a Function or Module in VB pengwinz Java Forum 2 17-May-2007 20:02
To post messages / click Buttons of a Java Jar App using code Jun0 C Programming Language 1 06-Jan-2007 14:44
To post messages / click Buttons of a Java Jar App using code Jun0 Java Forum 0 06-Jan-2007 11:14
Scalability in Java and C++ agx Miscellaneous Programming Forum 7 04-Feb-2006 15:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:34.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.