Results 1 to 7 of 7

Thread: String manipulation problem

  1. #1
    DaoK
    Guest

    String manipulation problem

    I have already done last week something like that and it work but now I do not know why I forget how to do it.
    I want to check all character of a string and find when it is numeral

    Exemple : Patrick Desjardins 12345
    I want to split that :
    var1 = Patrick Desjardins
    var2 = 12345

    This is an idea of what I want to do but I do not want anyone here to make the code but just to check the number verification here who still do not work :

    Code:
         sLigne = "Patrick Desjardins 12345"
         for (int i=0;i<sLigne.length ();i++)
        {
        leBon = sLigne.charAt(i);
            for (int j=0;j<=5;j++) // number can not be over 5
            {
            if (leBon.equals(j))
                System.out.println (leBon);
            }
        }
    Why it's do not work ?

  2. #2
    DaoK
    Guest
    the problem is on that line : if (leBon.equals(j))

    if leBon = "P"
    I want to check if P is a number so I equals it to j who is "1" for exemple but it do not work? What did I forgot ?

  3. #3
    DaoK
    Guest
    Is that so hard ? I think it's very basic question about string. What do I did wrong?

  4. #4
    DaoK
    Guest
    Why my code do not work, can someone copy and paste it and tell me why it do not work

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    let me ask you this...are all the strings you are splitting this way in the same format?

    Like:
    First Name Last Name Number

    ?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    DaoK
    Guest
    nop some time it can be only a name and some time 3 names

    Like :
    Patrick 24212
    Eric Josh Jr. 35312
    Mom 21224

    So i want to check all char in the line to find the first number to take it in a variable.

    But can you tell me what's wrong with the beginning of that code please.

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by crptcblade
    let me ask you this...are all the strings you are splitting this way in the same format?
    Posted by DaoK
    nop some time it can be only a name and some time 3 names
    Im looking at it this way. Wether it's one name or ten names the
    numeric sequence that comes right after the last name goes with that name, so with that said it shouldn't be too hard to parse out the data.

    Instead of writing the code for you. Looking at your code. i can see that in line two. You are iterating over the length of the String. Then in line 4 you are creating a nested loop that should iterate 5 times. if the string you are checking is long and you want to take time into consideration i think this would take a while becuase of all the looping. I dont think that using a StringTokenizer would be a good idea or work for that matter becuase you are looking to parse out all integers not just a single value like a space or a certain character. Im going to check out if a can do it with the PushbackReader class.

    Code:
         sLigne = "Patrick Desjardins 12345" // 1
         for (int i=0;i<sLigne.length ();i++) // 2
        {
        leBon = sLigne.charAt(i); //3
            for (int j=0;j<=5;j++) // number can not be over 5 // 4
            {
            if (leBon.equals(j)) // 5 
                System.out.println (leBon); // 6
            }
        }

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