Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how many occurences of specific values in a string? 1

Status
Not open for further replies.

mashadt

Instructor
Apr 23, 2005
33
ZA
OK - its getting late and my solutions are getting weerd.

In my program, a number of int values are added to a string

String output;
int value;

stuff happens

output += "*" + value;

In the end I would like to take this string and seperate out how many occurences of each int value there was, and presumably print this out.

Idealy, a string like *1*11*22
should be processed as having
"1 occurence of 1,
1 occurence of 11
1 occurence of 22"

The only way I can figure out just tells me how many occurences of each individual digit there is, so it would take the string *1*11*22 and tell me there were three 1's and two 2's in the string.

(I basically used a for loop to loop through all the characters in the string and check how many times each one occurs. But obviously that does not give me how many times each number occurs. )

What am I missing here?

 
You'll just have to split the String into an array (String.split("*") , or String.split("\\*") ) and then have a loop over the array which has a counter for each expected string type.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
If you're still at Java 1.3, StringTokenizer is your class.

Cheers,
Dian
 
Hey thanks sedj that is EXACTLY what I was looking for. Saves me having to muck around with linked lists.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top