Here is a tutorial that uses ArrayList structure in Java via XCode on a Mac laptop.
This program counts the occurrence of characters within the contents of a text file.
We have one input text file which we are going to read.
We use one ArrayList
We use another ArrayList
Breaking this down, it would be good to:
a) have method that reads the text file and does the report
b) have method to count occurrences of a character within a String
c) have method to count number of words within a String (and total characters in words)
This way you can report:
. occurrences of a character of interest within a line of the input text file
. total count and percentage of characters of interest within the whole text file
. total count and percentage of other characters within the whole text file
. total count of words within the whole text file
. average word size is ((total characters in words) / (total count of words))
So we’ll build up a FindOccurrences class this way.
Need to have rules about the input text file. Will have it that:
1) there is an optional first header record with number of records of interest
2) all records need {cr} (and/or {lf}) delimitation (as a text file should)
How do we obtain the filename? Will have it that:
(1) it can optionally be passed as a command line parameter
(2) if not we will prompt for it and allow a non-answer meaning it is inputfile.txt
Do we assume a and A (for instance) are the same thing? Will have it that:
(a) assume as a default a and A are to be treated as one entity for searching purposes
(b) there is a, albeit awkward, way to say that a and A are separate, via (2) above
Let’s see some Java programming code for this, and how it works …
Download source code and rename to FindOccurrences.java
If this was interesting you may be interested in this too.