This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
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
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"
Sheriff

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

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


Dave
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: http://aspose.com/file-tools
 
subject: Strings
 
Similar Threads
JUnit and Cactus using strutstestCase
Automatically reconnect to server ?
printing on an applet
Assert stmt in Eclipse
Dynamic Data Types in Eclipse