CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2007
    Posts
    87

    Stupid parsing question

    Hey,

    I'm being an idiot and I can't figure out how to do parse and get an integer from a string. Could someone give me an example please?

    Something like:

    "The action you going to take costs 5 dollars, and will take up to 3 days to complete"

    How would I store the value "3" as an integer? The "5 dollars" can vary.

    Thanks!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Stupid parsing question

    To convert a string to an int use Integer.parseInt(..)

    Of course you have to extract the string representation of the number from the string first. You can use a regex to do that.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2007
    Posts
    87

    Re: Stupid parsing question

    Yeah, thats the part that I don't know how to do, the regex part

  4. #4
    Join Date
    Apr 2011
    Posts
    9

    Re: Stupid parsing question

    hmmm...

    Strting text = "The action you going to take costs 5 dollars, and will take up to 3 days to complete";
    String[] strin;
    String delimiter = " ";
    strin = text.split(delimiter)
    int number = Integer.parseInt( strin[strin.length-4] );

    This should work, assuming the number is always the 4th element from the rear side of the string.

    Probably with some modification this would also work
    http://www.java2s.com/Code/Java/Regu...xpressions.htm

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured