Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Frequency of ASCII Characters

 
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Mark Andy
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I would begin with your code in the "while" statement?
 
lowercase baba
Posts: 13001
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 73733
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...;)
 
Sheriff
Posts: 16576
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 73733
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Master Rancher
Posts: 4457
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted at: http://www.dreamincode.net/forums/topic/389052-frequency-of-ascii-issue/
 
Paper beats rock. Scissors beats tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic