Casting from Map to String

Discussion in 'Java' started by dolobran, Dec 15, 2004.

  1. dolobran

    dolobran Guest

    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
     
    dolobran, Dec 15, 2004
    #1
    1. Advertising

  2. dolobran

    Sudsy Guest


    > 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...
     
    Sudsy, Dec 15, 2004
    #2
    1. Advertising

  3. dolobran

    Peter Kirk Guest

    "dolobran" <> skrev i en meddelelse
    news:...
    > 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).
     
    Peter Kirk, Dec 15, 2004
    #3
  4. dolobran

    Peter Kirk Guest

    "Sudsy" <> skrev i en meddelelse
    news:z_Ovd.55$...
    >
    > > 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" );
     
    Peter Kirk, Dec 15, 2004
    #4
  5. dolobran

    Sudsy Guest

    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! :)
     
    Sudsy, Dec 15, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,834
    Andrew McDonagh
    Jan 8, 2005
  2. Replies:
    3
    Views:
    335
    Pete Becker
    May 12, 2007
  3. Wally Barnes
    Replies:
    3
    Views:
    540
    Wally Barnes
    Nov 20, 2008
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    587
    James Kanze
    Dec 20, 2009
  5. albert kao
    Replies:
    12
    Views:
    620
    Roedy Green
    Oct 7, 2011
Loading...

Share This Page