Casting from Map to String

D

dolobran

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
 
S

Sudsy

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...
 
P

Peter Kirk

dolobran said:
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).
 
P

Peter Kirk

Sudsy said:
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" );
 
S

Sudsy

Peter Kirk wrote:
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! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top