Solved

Tokenizing and Counting String

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 36
jar file executable 12 51
expectj telnet failing 5 35
configure dependency in POM for new database 3 25
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

785 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