So my problem is, my program has read a text file and display in a GUI the frequency of all characters ASCII . Now this is a homework assignment so I'm looking for pointers on how to do this when it comes down to ASCII and not just a-z. What should I start writing in the while statement. I will worry about displaying it in my GUI once i figure out exactly what to do here first.
You should think about what you want to do, in English or other natural language. Pretend you are talking to a 10yr old child, and giving them directions on how to solve your task. LITERALLY write out the steps.
When you've done that, you'll have a much better time understanding how to code it.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mark Andy wrote:I will worry about displaying it in my GUI once i figure out exactly what to do here first...
Well that's a VERY good idea. Well done, and have a cow for working it out.
In fact, I'd go even further: Eliminate EVERYTHING that doesn't have to do with frequency counting from your program for now.
For example: What's that tokenizeFile() method for?
Doesn't sound like counting to me, so get rid of it (for now).
Another tip: In Java, a char is a 16-bitunsigned number, which is hinted at in the docs for BufferedReader.read():
"Returns ... the character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached."
Which should hopefully suggest that read()might return a value greater than 255.
There are two possible solutions to this:
1. Make sure you check every character you read to ensure that it is between 0 and 255 before you try to count it.
2. Make your "counts" array 65536 elements long.
The second option obviously takes up a lot more space - 512K instead of 2K - but it will then hold the counts for ALL the characters you read without any checks; and 512K isn't much these days. And you can then simply read the first 256 (or 128) counters to get the statistics you want.
It's a good lesson to learn: For anything more involved than "Hello World", there is almost always more than one solution, and which one is "right" (or righter) is down to you.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here