• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a string with "x" number of letters in it, I want to record the occurrance of every letter, how do I do this? TIA.
Kevin
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the charat function of the String class to get the letter at each position in the string and then write a long case statement to increase the counters for each letter.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Count?
If you use the charAt(index) method of String, you can walk through the characters of the String one by one.
As you bump into a new character you can add it to a Map of some sort, perhaps a HashMap or a TreeMap, using the character itself as the key and the element can be the count of how many you have found.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin
sounds like a school assignment so most people probably will not just blindly give the answer - the whole point of going to school is to learn...
What do you have so far? any code written? or even a rough outline of what steps you need - show us what you have and we'll point you in the right direction.
In case you dont even know where to start here is a rough thought process to use...
Steps
--get the string
--look at each letter and remember how many times it occured
--report your findings
now you can break each of these into even smaller pieces until you find a piece you know how to do then build off of that...
get the string (depends on the assignment requirments)
-- either parse the command line argument or
-- prompt for it
count the letters:
--need to look at each letter of the string in order making sure to only count each letter once
-- need to keep track of each letter counted and the total number of occurances of each
report your findings (depends on requirments)
-- System.out.println .....
Then break these steps down again... Eventually you'll get to a point where it is a simple matter of writing a few lines of code to perform a step.
Not what you wanted but I hope it helps
 
kevin schmidt
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave, this isn't a school assignment, I'm past that already... I'm learning Java at work... just trying to figure somethings out for help with building certain applications. I program in SAP under the ABAP/4 environment, and they now have a java side in the portals end of SAP and they are starting to incorporate java into their web enablement of their transactions
[ February 05, 2002: Message edited by: kevin schmidt ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic