Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Help with program for counting using hashtable

Help with program for counting using hashtable

Scheduled Pinned Locked Moved Java
questionjavacomhelptutorial
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john kappas
    wrote on last edited by
    #1

    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

    R 2 Replies Last reply
    0
    • J john kappas

      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

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • J john kappas

        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

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        I meant add it with value 1, not zero.  ;P /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups