Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

Tokenizing and Counting String

I have a string which structure is like:

db@1.txt, db@2.txt, db@1.txt, db@4.txt, db@7.txt, db@4.txt

Say that string is in variable 'content' and I got it from the first part of my program. And what I would like to get as an ouput is each name with its count like this:

db@1.txt, 2
db@2.txt, 1
db@4.txt, 2
db@7.txt, 1

Like what HashMap does, but I think HashMap cannot provide counting, but I am not sure.
How can I do this? I need to get count, but not necessary to do sort. Any idea would be greatly appreciated.

Thanks!!
0
Juuno
Asked:
Juuno
  • 2
  • 2
3 Solutions
 
mrjoltcolaCommented:
A HashMap can be used to do this.

The string "db@1.txt" is the key to the hash, and the data you store is the count.

So each time you see a string, fetch its existing value from the hash and increment it.


0
 
Kevin CrossChief Technology OfficerCommented:
Agree with MrJoltCola.

You can use split method to separate the value into strings then put in HashMap<String, Integer>.  If already exists in HashMap, you will get back integer value already there:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html#put(java.lang.Object, java.lang.Object)

If that value is not null, you can update the value of that key in HashMap to current integer + 1.

Regards,
Kevin
0
 
ChristoferDutzCommented:
This should do the trick ... hope ther are no Systax errors, since EE is no good for coding ;-)
String str = "db@1.txt, db@2.txt, db@1.txt, db@4.txt, db@7.txt, db@4.txt";
String[] tokens = str.split(", ");
Map<String, Integer> count = new HashMap<String, Integer>();
for(String token : tokens) {
    if(count.get(token) == null) {
        count.put(token, new Integer(1));
    } else {
        count.put(token, new Integer(count.get(token).intValue() + 1));
    }
}
 
for(String token : count.keyValues()) {
    System.out.println(token + ", " + count.get(token);
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Couple of comments on code example which is similar to what I had and would suspect MrJoltCola was referring too.

str.split(", ");
// for more flexibility in case there could not be a space after each comma
str.split(",\\s?");

for(String token : tokens) {
    if(count.get(token) == null) {
        count.put(token, new Integer(1));
    } else {
        count.put(token, new Integer(count.get(token).intValue() + 1));
    }
}

// can shorthand like this as use logic with assumption key doesn't already exist, then handle exceptions
for (String token : tokens) {
                  Integer i = count.put(token, 1);
                  if (i != null) count.put(token, i+1);
            }

Also note, that if you simple type:
System.out.println(count);

The toString method of HashMap will give you this result automatically:
{db@4.txt=2, db@2.txt=1, db@7.txt=1, db@1.txt=2}

Regards,
Kevin
0
 
mrjoltcolaCommented:
>>since EE is no good for coding ;-)

LOL, we are still waiting for EE Studio v2, the snippet window will be 2 lines longer!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now