Solved

string to HashMap error Java

Posted on 2007-11-29
5
2,582 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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