Results 1 to 6 of 6

Thread: Using the split

  1. #1
    bm2bmw is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Angry Using the split

    I am having an issue using this the split to store into an array.

    My inrec = 12345^^^^MR~123456789^^^^^^KU^CX~90901234^^^^^

    I need to capture the number in MR~123456789 in the inrec fiedl above and check the first 3 digits:

    I have the following and it is breaking out the code and not doing anything:

    string [] array = inrec.split("\\^");
    acct = array[4].substring(array[4].indexOf("~"), array[4].length());

    when i display acct, i am getting nothing !! and the code halts and does not go to the next instructions line !!

    What I am doing wrong ?


    Thanks

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

    Default

    Quote Originally Posted by bm2bmw View Post
    when i display acct, i am getting nothing !! and the code halts and does not go to the next instructions line !!
    Mhm :confused: I am getting : ~123456789 --> array[4].indexOf("~")+1 --> 123456789

    Java Code:
    		String inrec = "12345^^^^MR~123456789^^^^^^KU^CX~90901234^^^^^";
    		String [] array = inrec.split("\\^");
    		String acct = array[4].substring(array[4].indexOf("~")+1, array[4].length());
    		System.out.println(acct);
    Maybe it its easier to use Pattern and Matcher? Do you have always MR~AndHereANumber?

  3. #3
    bm2bmw is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Yes. It will aslo have CX~ where i only need to capture the nymber after CX~ OR MR~ , how woudl you do that in a scanner. Thank you for the first response which i well try shortly. Thanks

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

    Default

    Quote Originally Posted by bm2bmw View Post
    how woudl you do that in a scanner.
    Java Code:
    		Scanner sc = new Scanner(.....);
    		while(sc.findWithinHorizon("(?:MR|CX)~(\\d+)", 0) != null){
    			System.out.println(sc.match().group(1));
    		}
    :confused:

  5. #5
    bm2bmw is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Thumbs up

    Thank yuo so much. I will let u know how it went.

  6. #6
    bm2bmw is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Thumbs up

    String inrec = "12345^^^^MR~123456789^^^^^^KU^CX~90901234^^^^ ^";
    String [] array = inrec.split("\\^");
    String acct = array[4].substring(array[4].indexOf("~")+1, array[4].length());


    Thsis worked perfectly. Thanks

Similar Threads

  1. How to use split?
    By jeff_limsy in forum New To Java
    Replies: 3
    Last Post: 12-29-2010, 09:23 AM
  2. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  3. String Split
    By sarovarc in forum New To Java
    Replies: 6
    Last Post: 04-19-2010, 05:06 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 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
  •