Results 1 to 4 of 4
  1. #1
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default A problem with converting String to Int.

    I have a ArrayList of strings. I need to convert them to integer if possible and compare to certain value witch is also integer. Here is what i do:


    for(int j=0;j<sortList1.size();j++){

    try {
    iValue = Integer.parseInt(sortList1.get(j));
    }
    catch (NumberFormatException ex) {
    }

    if
    (iValue==t) {t=t+1;}

    The problem is that the code continues to work only till it didnt met any non convertable strings. When it meets something like "abc" it throws exception and doesnt check other elements from array. How can i solve this problem? Plz help!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: A problem with converting String to Int.

    That cannot be ?! Show us a full code example!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: A problem with converting String to Int.

    When it meets something like "abc" it throws exception and doesnt check other elements from array. How can i solve this problem?
    Some how you need to detect if the String can be converted to an int. What do you want to do if it can't?
    One way is to let the parseInt method do the testing and throw the exception which you catch and handle (for example just print a message and continue the loop to get the next item in the list).
    Another would be to scan all the characters in the String and check if they are valid digits.
    Last edited by Norm; 12-19-2011 at 11:18 PM.

  4. #4
    hell00 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: A problem with converting String to Int.

    Thanks! I'll try that.

Similar Threads

  1. converting binary to string
    By stegano in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 07:20 PM
  2. Converting to String
    By darek9576 in forum New To Java
    Replies: 1
    Last Post: 03-13-2010, 10:07 PM
  3. Is it OK to do this... (converting int to string)
    By Psyclone in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 05:51 PM
  4. Converting a String into a array.
    By taraxgoesxboom in forum New To Java
    Replies: 12
    Last Post: 02-22-2009, 05:29 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
  •