Re: Read in & count characters from a text file
Supersedes: <autovivificate-20070804231921@ram.dialup.fu-berlin.de>
"Jay Cee" <itsjayceecee@hotmail.com> writes:
My problems seems to be I cannot "check the map" if the
character exists and if it does exist how do I get at the value
to increment it.
You might use something like
class NumericMapUtils
{ public static <D> void addTo /* autovivificate the value to 0 */
( final java.util.Map<D,java.lang.Integer> map, final D d, final int i )
{ map.put( d, i +( map.containsKey( d )? map.get( d ): 0 )); }}
and a sorted map
java.util.TreeMap<java.lang.Character,java.lang.Integer> map;
then add each text like
NumericMapUtils.addTo<java.lang.Character>( map, 'a', 1 );
NumericMapUtils.addTo<java.lang.Character>( map, 'c', 1 );
NumericMapUtils.addTo<java.lang.Character>( map, 'n', 1 );
NumericMapUtils.addTo<java.lang.Character>( map, 'x', 1 );
(I have not tested this. Possibly, the "<java.lang.Character>"
type argument can be omitted.)
Then iterate: ?for( final java.lang.Character key: map.keySet() )?
For files of arbitrary size, use java.math.BigInteger instead
of java.lang.Integer.
Supersedes: <autovivificate-20070804231921@ram.dialup.fu-berlin.de>