Solved

Tokenizing and Counting String

Posted on 2009-07-09
6
298 Views
Last Modified: 2012-05-07
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
Comment
Question by:Juuno
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 168 total points
ID: 24813579
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
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24813778
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
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 166 total points
ID: 24813817
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
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 166 total points
ID: 24813946
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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24813952
>>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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question