Solved

Tokenizing and Counting String

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 132
Systems talking to each other 5 125
base64 decode encode 12 121
how to add new optional parameter to JSP 1 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 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

12 Experts available now in Live!

Get 1:1 Help Now