Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By SurfMan
  • 1 Post By Tolls
  • 1 Post By SurfMan

Thread: separate the numbers into a string

  1. #1
    maj_ is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Post separate the numbers into a string

    hi , Is it possible to separate the numbers into a string and put each into a variable?

    for example :


    string "a 2563 ho 3367852 lop 555202"

    n1=2563;
    bp=3367852;
    n3=555202;



    thanks.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: separate the numbers into a string

    Quote Originally Posted by maj_ View Post
    hi , Is it possible to separate the numbers into a string and put each into a variable?
    for example :
    string "a 2563 ho 3367852 lop 555202"
    n1=2563;
    bp=3367852;
    n3=555202;
    thanks.
    There are several ways of doing this. Quickest way is with a regular expression, but requires understanding of regular expressions. If you know your regexes, which I doubt, try this: \b(\d+)\b. If you don't, nevermind.

    Second way is to loop over each character and check if it's a digit or not. Character.isDigit() is your friend there. Keep adding those digits to a temporary String until it is not a digit anymore. Then parse the temporary String using Integer.parseInt().
    maj_ likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: separate the numbers into a string

    Well, I would split() on the spaces, then iterate over the String[] that you get back, and parseInt on each of them, throwing away any that chuck up an exception.
    But then I do have a habit of going for the "brute force and ignorance" methodology...
    :)
    maj_ likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: separate the numbers into a string

    Quote Originally Posted by Tolls View Post
    Well, I would split() on the spaces, then iterate over the String[] that you get back, and parseInt on each of them, throwing away any that chuck up an exception.
    But then I do have a habit of going for the "brute force and ignorance" methodology...
    :)
    If it works it ain't stupid :)
    maj_ likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: separate the numbers into a string

    You're actually trying to separate a String into several integers?

  6. #6
    maj_ is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Default Re: separate the numbers into a string

    Yes, exactly .

  7. #7
    maj_ is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Default Re: separate the numbers into a string

    Quote Originally Posted by cronnin View Post
    You're actually trying to separate a String into several integers?
    Yes, exactly .

  8. #8
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: separate the numbers into a string

    Brute force would probably go like:

    -split into chars
    - start loop
    - check if (int)character is between 65-90 and 97-122
    - if yes add char to character ArrayList
    - if no, it means you have a separate array of letters; convert it to a String and add onto String ArrayList; clear char ArrayList;
    or it means you have a new forbidden character in a sequence. You either add a flag checking last character, or you check if char ArrayList is empty.

    edit: now I see I've got it to exclude only words, and you asked for numbers, but logic would be similar
    Last edited by cronnin; 02-03-2017 at 03:09 AM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: separate the numbers into a string

    1. For first character in string, set k = 0 for is digit, or 1 for is not digit.
    2. Then String.split("\\D+").
    3. Then convert array from k to end using Integer.parseInt().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: separate the numbers into a string

    Quote Originally Posted by jim829 View Post
    1. For first character in string, set k = 0 for is digit, or 1 for is not digit.
    2. Then String.split("\\D+").
    3. Then convert array from k to end using Integer.parseInt().
    Although the above algorithm only took about 5 lines of code to implement, a more specific algorithm using character indices was about 2.5 times as fast. As much as I enjoy regular expressions they do incur a significant overhead. My faster algorithm was about 22 lines and could probably be improved.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Join Date
    Mar 2017
    Posts
    1
    Rep Power
    0

    Default Re: separate the numbers into a string

    (Removed code)

    Try this
    Last edited by Tolls; 03-08-2017 at 03:23 PM. Reason: removed "solution"

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: separate the numbers into a string

    Try this
    @preethi_gorgeous Please don't spoonfeed OPs with complete solutions. Rather help them work out their problems.
    When you do post code, make sure it is properly documented and wrapped in code tags.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. adding numbers in a string?
    By brycepower1 in forum New To Java
    Replies: 14
    Last Post: 08-19-2011, 06:39 PM
  2. Separate Numbers
    By gozuhair in forum New To Java
    Replies: 38
    Last Post: 07-21-2011, 12:42 PM
  3. Replies: 6
    Last Post: 12-06-2010, 10:26 AM
  4. add all the numbers in a string
    By gibson.nathan in forum New To Java
    Replies: 29
    Last Post: 09-30-2009, 07:14 PM
  5. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 01:12 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
  •