Solved

string to HashMap error Java

Posted on 2007-11-29
5
2,579 Views
Last Modified: 2011-09-20
Hi All,

I am having issues with this , I hope u guys can help me out
<% java.util.HashMap searchHash = new java.util.HashMap();%>
<%String selectedvalue = request.getParameter("var"); //
 String selectedfield = request.getParameter("selectedfield");%>
   
  <% searchHash.put(selectedfield,selectedvalue);
 
  System.out.println("value:"+(java.util.HashMap)searchHash.get(selectedfield));  /// ERROR , java.lang.ClassCastException

Can you tell me why? I am really confused

Thanks in advance
0
Comment
Question by:prk_usa
  • 2
  • 2
5 Comments
 
LVL 27

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 200 total points
ID: 20379963
Your HashMap doesn't return a HashMap, it returns a String.  So cast the result of searchHash.get to (String) .
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 20379975
you don't really need to cast it at all.

  System.out.println("value:"+searchHash.get(selectedfield));
0
 

Author Comment

by:prk_usa
ID: 20379979
Tanks for u r response mrcoffee365.
I am not following you, I am sorry.

but

searchHash.get(selectedfield));  ---> returning String

I want this to be type casted to HashMap , because I have to pass thi into a function, and that function accepts

HashMap only
0
 
LVL 92

Expert Comment

by:objects
ID: 20379987
> because I have to pass thi into a function, and that function accepts

then just pass your hashmap, eg.

myMethod(searchHash);
0
 

Author Comment

by:prk_usa
ID: 20380010
oh ok , I will try it out and let you know and Thanks for your response.

 I really appreciate.
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
Display current date,month,and year to JTextField 3 47
library class in java 1 90
attribute vs parameter and setter vs add method 17 83
bean mapper frameworks 5 59
Facing problems with you memory card? Cannot access your memory card? All stored data, images, videos are lost? If these are your questions...than this small article might help you out in retrieving your lost or inaccessible data.
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

18 Experts available now in Live!

Get 1:1 Help Now