Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Casting from Map to String

Reply
Thread Tools

Casting from Map to String

 
 
dolobran
Guest
Posts: n/a
 
      12-15-2004
I am trying to get the key value from a Map and cast to a String.

I am getting a class cast exception.

Map queryArgs = request.getParameterMap();

urlSurveyIdInt = ((Integer) session.getAttribute("surveyid")
).intValue();

The intended behavior is to capture the URL parameter surveyid value
from a URL such as this:

http://myDomain.com/survey.jsp?surveyid=1
Can someone help?

Thanks in advance

 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      12-15-2004

> urlSurveyIdInt = ((Integer) session.getAttribute("surveyid")
> ).intValue();
>
> The intended behavior is to capture the URL parameter surveyid value
> from a URL such as this:
>
> http://myDomain.com/survey.jsp?surveyid=1
> Can someone help?
>
> Thanks in advance


You can't just cast one object type to another. In this case, you
could try something like this:

urlSurveyIdInt = new Integer( session.getAttribute( "surveyid" )
).intValue();

This is very basic Java, BTW. Questions like this might be better
directed to comp.lang.java.help in future.

ps. Be sure to catch the NumberFormatException...
 
Reply With Quote
 
 
 
 
Peter Kirk
Guest
Posts: n/a
 
      12-15-2004
"dolobran" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed) ups.com...
> I am trying to get the key value from a Map and cast to a String.


Are you sure that is what you are trying to do? Your use if the terms is a
little ambiguous also: with a map one talks about "keys" and "values"; to
talk about "key values" is a bit confusing.


> I am getting a class cast exception.
>
> Map queryArgs = request.getParameterMap();
>
> urlSurveyIdInt = ((Integer) session.getAttribute("surveyid")
> ).intValue();


This is not from the request. Are you trying to get it from the session
(HttpSession) ?


> The intended behavior is to capture the URL parameter surveyid value
> from a URL such as this:
>
> http://myDomain.com/survey.jsp?surveyid=1
> Can someone help?


Something like this?

String s = request.getParameter( "surveyid" );
if ( s != null ) {
int i = Integer.parseInt( s );
}

(Could still get a NumberFormatException if the string is not convertible).

 
Reply With Quote
 
Peter Kirk
Guest
Posts: n/a
 
      12-15-2004

"Sudsy" <(E-Mail Removed)> skrev i en meddelelse
news:z_Ovd.55$(E-Mail Removed)...
>
> > urlSurveyIdInt = ((Integer) session.getAttribute("surveyid")
> > ).intValue();
> >
> > The intended behavior is to capture the URL parameter surveyid value
> > from a URL such as this:
> >
> > http://myDomain.com/survey.jsp?surveyid=1
> > Can someone help?
> >
> > Thanks in advance

>
> You can't just cast one object type to another. In this case, you
> could try something like this:
>
> urlSurveyIdInt = new Integer( session.getAttribute( "surveyid" )
> ).intValue();
>
> This is very basic Java, BTW. Questions like this might be better
> directed to comp.lang.java.help in future.
>
> ps. Be sure to catch the NumberFormatException...


Also, if he is using the session, why not just store the Integer object
there? Then he can do

int myInt = ((Integer)session.getAttribute( "surveyid" )).intValue;

But if it is the request he is using, why not rather

int myInt = Integer.parseInt( request.getParameter( "surveyid" );


 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      12-15-2004
Peter Kirk wrote:
<snip>
> Also, if he is using the session, why not just store the Integer object
> there? Then he can do
>
> int myInt = ((Integer)session.getAttribute( "surveyid" )).intValue;
>
> But if it is the request he is using, why not rather
>
> int myInt = Integer.parseInt( request.getParameter( "surveyid" ) );


Both good points! I was presuming (never a good thing!) that the OP
actually /wanted/ the Integer object at some point further on. If not
then simply using Integer#parseInt is a lot more appropriate.
Just don't try to use Integers in your Struts forms!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Support Map<String, String> & Map<String, MyString> albert kao Java 12 10-07-2011 05:48 PM
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Is this possible: map of references (map<string&, string&>) asclearuc@gmail.com C++ 3 05-12-2007 03:31 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments