Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Frequency of ASCII Characters  RSS feed

 
Mark Andy
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my problem is, my program has read a text file and display in a GUI the frequency of all characters ASCII [256]. 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.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mark Andy
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I would begin with your code in the "while" statement?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should begin with not writing code at all.

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.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that child of yours is too old, Fred. Try explaining it to a 5‑year old. Or, if you want somebody really hard to explain things to, me.

ASCII characters run from 0...0x7f(=127), not up to 256.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't be so hard on yourself, Campbell. I'd bet I could explain the "HelloWorld" program to you in less than two weeks...;)
 
Junilu Lacar
Sheriff
Posts: 11153
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:ASCII characters run from 0...0x7f(=127), not up to 256.

Since he declared an array int[256] he's really going for 0..255. My guess is that they're looking at an 8-bit "extended ASCII" code set.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . I'd bet I could explain the "HelloWorld" program to you in less than two weeks...;)
I was learning Java for much more than two weeks before I even saw a Hello World program.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-bit unsigned 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.

HIH

Winston
 
Norm Radder
Ranch Foreman
Posts: 2240
28
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!