Yeah today i'm sharing a little java program who can scan any editable file in your system and count all the alphabet character from it and show the result in a chart renderer in an html document


  • 0
    @ostream thanks
  • 0
    HashMap for the win
  • 0
    @Ranchonyx what do you mean?
  • 1
    @Afrographics I mean you've got to save the amount of characters somewhere.

    And I used to do
    HashMap<Character, Integer>
    For using the character as key and then incrementing the count if character occurs
  • 0
    @Ranchonyx i actually created a class for that 🥲
  • 1
    It looks great! The only thing I would change are the file names. CharCount and CountChar could be easily confused. Other than that, good job!
  • 3
    Nice work! A little suggestion though, getIndexFromCharCounter will take on average N/2 steps to locate the relevant counter where N is the number of characters you recognize. We call this O(n) or linear time complexity. If instead of the counter class you'd use a HashMap<char, int> then this time would be the 2-based logarithm of N, or in big-O notation O(log(n)), which is a lot faster. Internally, HashMap uses a clever datastructure to achieve this, but you don't have to know how it works in order to use it.
  • 3
    The other thing to take away from this is that a good class - such as HashMap - has a distinct reason to be a class. Three common ones are:

    1. To conceal ("encapsulate") some elaborate mechanism or datastructure to simplify calling code

    2. To implement some interface or extend a base class. If you don't know what these are, they enable you to treat objects of different types that share some functionality as a common type, for example put them together in a HashMap

    3. To store multiple values if you don't want to specify them or keep track of all of them, either because the list may change or because there are many. For example, DateTime has a bunch of fields for year, month, day, etc. which you always want to store together, and some utility methods that all operate on this set of values.
  • 2
    An additional rule of thumb is that you shouldn't store the data that identifies an object in the object itself unless you need it after you've found the object you were looking for.

    In your case, CharCount shouldn't store the character itself because you don't need it once you have the right object. Instead, CountChar should have a HashMap<char, CharCount> which you can use to get the right counter on the first try.

    After that's done you will realize that CharCount stores a single int and exposes the increment operation, and the regular int datatype can do both of those just fine, so actually you could just use a HashMap<char, int>.
  • 0
    datastructure - German?
  • 0
    Could that graph be any lighter?
Add Comment