Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help converting to a String

Status
Not open for further replies.
Jan 8, 2001
163
US
Hello all. I have a hashtable. It consists of the keys which are usernames and the other user data in tokenized format.

ie/ SALLY 1423{%}SALLY{%}JOHNSON{%}ABC COMPANY{%}
JOAN 1432{%}JOANUS{%}SMITH{%}CDE COMPANY{%}

I'm trying to create a table on my jsp page that lists the username and each of the additional fields separately. Then problem is that when I pass the second field of the hashtable to the StringTokenizer I get this error.

Explicit cast needed to convert java.lang.Object to java.lang.String.

Apparently the non-key field from the hashtable is not treated as a string. Does anyone know what method I can call to convert this to a string and send it to StringTokenizer()? My code is below.

Please Note: I declare the bean used to get the user params prior to all of this code...

...
<%!
public Hashtable ht_userdata;
Enumeration usernames;

public String str = &quot; &quot;; %>
<%
ht_userdata = new Hashtable();
UserData UDO = new UserData();

//user_search method returns the hashtable
ht_userdata = UDO.user_search(udb.getcriteria(), udb.getstype());
usernames = ht_userdata.keys();

while(usernames.hasMoreElements()) {
str= (String) usernames.nextElement();
String s = ht_userdata.get(str);
StringTokenizer st = new StringTokenizer(s, &quot;{%}&quot;); //ERRORS HERE

while (st.hasMoreTokens()) {
String userid = st.nextToken();
String password = st.nextToken();
String u_startdate = st.nextToken();
String u_enddate = st.nextToken();
String company = st.nextToken();
String u_firstname = st.nextToken();
String u_lastname = st.nextToken();
}
%>
... blah blah blah
<%
}
%>

Any help would be greatly appreciated.

Thanks!
CrystalVisualBOracle :)
 
get() always returns on Object, so you need to cast it to a String.

String s = (String)ht_userdata.get(str);

That will solve your problem, but I don't think you are using the delimeter in StringTokenizer correctly. If I remember correctly (can't test code right now), the tokenizer will only work properly with a single character as the delimeter. It will compile if you use {%} but will yield unexpected results. You could split the string up using &quot;{&quot; and then delete the &quot;%}&quot; at the start of each token (you will have to worry about {'s that are not part of &quot;{%}&quot;).

I hope this helps.

 
Hi, MetalEye is half right. The casting part is correct but the StringTokenizers part is wrong. When I tokenize with &quot;{%}&quot;, the program will tokenize your String using &quot;{%}&quot; as a seperate character each. E.g.:-

String abc = Hello{world%Hello};

After passing this String to a StringTokenizer, you will get the following Tokens;Hello, world and Hello

---------------------------------------------

String abc = Hello{%}worldHello;

You will get Hello and worldHello. So the best way is to always use 1 delimeter.

Regards,
Leon
 
Yes you can use Metal Eye's suggestion to remove the error Explicit cast needed to convert java.lang.Object to java.lang.String.
As for the tokenizing part i have written a small java class which will help you tokenize the way you want it to happen.
I just tested it and it works fine.
so go ahead an use it if you like it.


import java.util.Vector;

public class MyTokenizer
{
public Vector getScopeTokens(String s,String delimeter)
{
int dellength=delimeter.length();
int firstindex=0;
String temp=&quot;&quot;;
Vector ll=new Vector();
int pos=0;
int posvalholder=0;
int cominginfirsttime=0;
while(pos!=-1)
{
posvalholder=pos;
pos= s.indexOf(delimeter,firstindex);
if(pos!=-1)
{
temp=s.substring(firstindex,pos);
ll.add(temp);
temp=&quot;&quot;;
firstindex=pos+dellength;
}
else if(cominginfirsttime!=0)
{
temp=&quot;&quot;;
temp=s.substring(posvalholder+dellength);
ll.add(temp);
}
cominginfirsttime=1;

}
for(int i=0;i<ll.size();i++)
{
System.out.println(&quot;token[&quot;+i+&quot;]&quot;+(ll.get(i)).toString());
}
return ll;
}
public static void main(String args[])
{
MyTokenizer st= new MyTokenizer();
st.getScopeTokens(&quot;1423{%}SALLY{%}JOHNSON{%}ABC COMPANY{%}&quot;,&quot;{%}&quot;);
}

}




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top