Click to See Complete Forum and Search --> : splitting up an object


Rabbit3cat
03-04-2006, 09:04 AM
Hi i have a list of object, in the form emailAddress=float, like below

[anne@gmail.com=3.245392,steven@hotmail.com=0.32849...]

What i need to do is split these objects up so that i just have the email address, so in this example i would just have
anne and steven

I have been trying to use a string tokenizer and splitting the object at the = sign

List list1 = new ArrayList();

Iterator it = likely.iterator();

while(it.hasNext()) {
Object e = (Object)it.next();

StringTokenizer st = new StringTokenizer((String)e, " =");
while(st.hasMoreTokens())
list1.add(st.nextToken());
}

System.out.println("List " + list1);



This however is not working, does anybody know of a way i can implement this, id appreciate any help
Thanks

Khalid Ali
03-04-2006, 09:14 AM
the code below will work. See the problem is that tokenizer returns 2 tokens for each split hence putting eamil and the value on the right side of the splitter character in the list where as u only want the first value to be added...

while(it.hasNext()) {
Object e = (Object)it.next();

StringTokenizer st = new StringTokenizer((String)e, " =");
if(st.hasMoreTokens()){
String token = String.valueOf(st.nextToken());
list1.add(token);
System.out.println("List " + token);
}
}

Rabbit3cat
03-04-2006, 09:23 AM
Thanks Khalid Ali. Im trying out that code now but i am getting the error:
"Exception in thread "main" java.lang.ClassCastException: java.util.HashMap$Entry
at PrecisionRecall.Recall.getRecall(Recall.java:88)
at PrecisionRecall.main.main(main.java:31)"

I think the problem is trying to tokenize an object with a string tokenizer but i am not sure, do you have any ideas?

Rabbit3cat
03-04-2006, 09:46 AM
I tested that code on objects i have on the list in a seperate file and it worked fine. I am adding the objects to the list from a hash map


List store = new ArrayList();
Set set = map1.entrySet();
Iterator iter = set.iterator();
while(iter.hasNext()) {
Object o = iter.next();

store.add(o);

}


Collections.sort(store, val);
Collections.reverse(store);

Iterator iter1 = store.iterator();

while(iter1.hasNext() & i < k +1) {

Object f = iter1.next();
likely.add(f);
System.out.println( f);
i++;
}
}

I am then tokenizing the objects in the list "likely"
so the problem must be with how the objects are being added to the list, but i cannot see where it is, can anybody tell from this code where the problem might be?

Rabbit3cat
03-04-2006, 10:00 AM
Got it working! Thanks again

Khalid Ali
03-04-2006, 11:25 AM
u are welcome....just remember fir future that tokenizer doesn't know that you want the first value or the last or whichever..so you will have to make sure that you get the correct value out of it..:-)