Results 1 to 9 of 9
  1. #1
    vinikz is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default String to Integer Conversion in JSP

    Hi,
    I have a string. Iam trying to convert this to integer. But iam getting error message. Here iam displaying my code:
    <%
    int count;
    String num=request.getParameter("numFields"); // this value coming from my textbox
    out.println("num : "+num);
    count = Integer.parseInt(num); // In this line error coming
    out.println("Count : "+count);
    //for (int x=0; x<count; x++)
    //{
    %>

    Pleae help me.

    Thanking you,
    Vinith

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    What is displayed by out.println(num)? Is num a String of numeric characters or ?
    Should your code test for num == null?
    What is the error message?

  3. #3
    thusa is offline Member
    Join Date
    Jul 2008
    Posts
    19
    Rep Power
    0

    Default

    Before calling parseInt() method, you better check whether the value is null, since this method would throws a NPE.

    It would be easy for us, if you had pasted the error message here.

  4. #4
    vinikz is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi,

    I given condition for checking null value. Now my program is running well.

    Thank you so much.......

  5. #5
    thusa is offline Member
    Join Date
    Jul 2008
    Posts
    19
    Rep Power
    0

    Default

    Glad that it's working.

  6. #6
    viswa is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    i have string that contains 4.0 take from database.then how i convert it into int value.
    i write the code in jsp,by using Integer .parseInt() i got error number format exception
    how can i solve this.......

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Start your own thread rather than resurrecting an old one?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    "4.0" is not a integer. It looks like a double or float.
    Parse with a method from one of those wrapper classes and then convert that value to an int by casting.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    And after doing that you may want to rethink your db structure if you're storing numbers as strings.

Similar Threads

  1. Replies: 4
    Last Post: 03-25-2008, 01:16 PM
  2. bytearray to string conversion
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 11:39 AM
  3. Method for String to int conversion
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 07:35 PM
  4. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 04:59 PM
  5. String to Date conversion
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 08:16 PM

Posting Permissions

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