Parsing String to Integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lkjayarathna
    New Member
    • Nov 2008
    • 3

    #1

    Parsing String to Integer

    How do I transfer numeric values of a string which has both numbers, character and symbols to integer

    Thanks
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    Hi,
    The String API has methods that you can easily use to find any characters you want from the string. Once you have for example found a number use the String.parseInt () method to convert it to an integer.

    Consider the methods charAt(), contains(), subString() e.t.c.

    You can get a better response if you tell us exactly what you are trying to do and where you are particularly stuck.

    You may want to brush up on the String class and the methods therein.

    Regards,

    Alex.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      You need to get the numbers out first.

      Hint: Splitting the string on "\\D" gives an array containing all the numbers and a lot of empty strings.

      Comment

      • lkjayarathna
        New Member
        • Nov 2008
        • 3

        #4
        Thanks guys for the quick reply,

        What I am trying to do is, I have a List which shows few string & int in one row as a one string.

        So i want to get a substring of the the final string, which contains the (initial int value)numeric value i want. So then I can use it to do the rest of the calculations.

        But I cannot use Integer.parseIn t(mysubstring) to pass tp a string because it contains both numeric and characters.

        Thanks

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Reread reply #3 again; a slightly better regular expression to use is "\\D+"; it
          results in a smaller array.

          kind regards,

          Jos

          Comment

          • lkjayarathna
            New Member
            • Nov 2008
            • 3

            #6
            I have no idea what u guys talking about D

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Go to Google and search for String.java.

              Click the first link and find the split() method explanation. (here's a link)

              While you may think you have to learn an entirely new language, Jos and r0 have already given you the string needed to split your String. The rest is just some reading and code attempts.

              Hope this helps!

              Comment

              Working...