This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logic problem

 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone
I am not able to understand this program, It counts the repeated characters in the given string.
Can you tell me how it is doing so, what is the logic behind this.
What 65,91,97,123 denotes here ?
Thanks in advance!!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those numbers are character codes (ASCII). It only counts characters in the range 65 to 91 and 97 to 123 (inclusive), which are letters A to Z and a to z, and the characters [ and {. The fact that it includes [ and { is probably a bug. It should have been < 91 and < 123.
 
Campbell Ritchie
Sheriff
Posts: 48930
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you find that code? It would have been far better to use a method like this than those error‑prone <= operators.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
those are what's called "magic numbers", because you have to have some magic to know what they mean. They should be avoided at all cost.
 
Randall Twede
Ranch Hand
Posts: 4391
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
awesome Campbell! thanks for sharing that for when i might need it!
 
Campbell Ritchie
Sheriff
Posts: 48930
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic