Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2592
  • Last Modified:

string to HashMap error Java

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
prk_usa
Asked:
prk_usa
  • 2
  • 2
2 Solutions
 
mrcoffee365Commented:
Your HashMap doesn't return a HashMap, it returns a String.  So cast the result of searchHash.get to (String) .
0
 
objectsCommented:
you don't really need to cast it at all.

  System.out.println("value:"+searchHash.get(selectedfield));
0
 
prk_usaAuthor Commented:
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
 
objectsCommented:
> because I have to pass thi into a function, and that function accepts

then just pass your hashmap, eg.

myMethod(searchHash);
0
 
prk_usaAuthor Commented:
oh ok , I will try it out and let you know and Thanks for your response.

 I really appreciate.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now