File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic


kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
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.
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
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.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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...
--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

Joined: Aug 24, 2001
Posts: 85
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 ]
I agree. Here's the link:
subject: Strings
It's not a secret anymore!