Solved

Tokenizing and Counting String

Posted on 2009-07-09
6
259 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now