Results 1 to 4 of 4
  1. #1
    monika is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default arraylist to integer array

    Hello,


    I am extracting some data from database(which is a integer field in the database) and I am storing that data into a arraylist.
    Now I want to find to find some further operation I want to store it in a integer array---

    But when I am trying to so I am getting a error message.I have done all this in a JSp page

    java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer


    <% ArrayList ar=new ArrayList(); %>
    <% while(rs.next()) { %>

    <%


    String strprodvalue = rs.getString("first");
    out.println(strprodvalue);
    ar.add(strprodvalue);
    //int[] ints=new int[ar.size()];

    %>

    <BR>
    <%
    }
    %>


    <%

    Object ia[] = ar.toArray(); //get array
    int sum = 0;
    for(int i=0; i<ia.length; i++)
    {
    sum+=((Integer)ia[i]).intValue();

    }

    out.println("Sum is :" + sum);

    %>


    Can you please help me how to convert arraylist into a integer array

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    For starters don't do that in a JSP.

    If the data coming from the db is an integer, why are you using getString() and not getInt()?

  3. #3
    monika is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    Thank you for your help.

    Can you please suggest me how can I write this method in classes instead of JSP and how can I access that?

    Thank You

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Write Java?
    I'm sorry, but that seems a bit of an odd question.
    I presume you learnt Java before moving onto JSPs.

Similar Threads

  1. How do you store a random integer in an array?
    By Unknown in forum New To Java
    Replies: 6
    Last Post: 03-21-2010, 10:40 PM
  2. An Array of different integer values
    By lithium002 in forum New To Java
    Replies: 7
    Last Post: 12-04-2009, 05:25 AM
  3. change string to array of integer
    By prof.deedee in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 02:47 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Passing integer array from javascript to java
    By lavanyashastri in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •