Help with program for counting using hashtable
-
I want to create a programm that would read data from a file and will put each distinct item in a hashtable,counting how often it appears.I want to count first how often single characters or numbers appear,then how often the combination of two characters appear,three characters etc. More specifically... How can I use hashtable to count the appearances of some items in a file.For example how often is the letter a used,letter b etc.I am using hashtable.class in java.util.* ,but what I can not do is associate each letter that I am reading with a place in the hashtable. For example the following code creates a hashtable called numbers with the default capacity and load factor.Then one-two-three are used as the keys and 1-2-3 as the values. Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3)); I want my hashtable to read a file one character or one number at a time and count their appearances. The data would most probably be in the form abc abcd a abce ecf e fg or 123 12346 12 348 7 2 3456 52 364725 Thanks a lot for suggestions to my previous question as well as to future ones. jkouris@hotmail.com
-
I want to create a programm that would read data from a file and will put each distinct item in a hashtable,counting how often it appears.I want to count first how often single characters or numbers appear,then how often the combination of two characters appear,three characters etc. More specifically... How can I use hashtable to count the appearances of some items in a file.For example how often is the letter a used,letter b etc.I am using hashtable.class in java.util.* ,but what I can not do is associate each letter that I am reading with a place in the hashtable. For example the following code creates a hashtable called numbers with the default capacity and load factor.Then one-two-three are used as the keys and 1-2-3 as the values. Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3)); I want my hashtable to read a file one character or one number at a time and count their appearances. The data would most probably be in the form abc abcd a abce ecf e fg or 123 12346 12 348 7 2 3456 52 364725 Thanks a lot for suggestions to my previous question as well as to future ones. jkouris@hotmail.com
How can I use hashtable to count the appearances of some items in a file.
Query the hashtable for the key before you stick it in. It it doesn't exist, add the key with value 0. If it exists, get the key's value, add 1 to it, and reinsert it into the hashtable. When you're done, the values of all keys will be the # times they were added. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com -
I want to create a programm that would read data from a file and will put each distinct item in a hashtable,counting how often it appears.I want to count first how often single characters or numbers appear,then how often the combination of two characters appear,three characters etc. More specifically... How can I use hashtable to count the appearances of some items in a file.For example how often is the letter a used,letter b etc.I am using hashtable.class in java.util.* ,but what I can not do is associate each letter that I am reading with a place in the hashtable. For example the following code creates a hashtable called numbers with the default capacity and load factor.Then one-two-three are used as the keys and 1-2-3 as the values. Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3)); I want my hashtable to read a file one character or one number at a time and count their appearances. The data would most probably be in the form abc abcd a abce ecf e fg or 123 12346 12 348 7 2 3456 52 364725 Thanks a lot for suggestions to my previous question as well as to future ones. jkouris@hotmail.com
I meant add it with value 1, not zero. ;P /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com