PDA

Click to See Complete Forum and Search --> : String manipulation problem


DaoK
Nov 20th, 2001, 01:14 PM
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 :


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 ?

DaoK
Nov 20th, 2001, 01:22 PM
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 ?

DaoK
Nov 20th, 2001, 04:05 PM
Is that so hard ? I think it's very basic question about string. What do I did wrong?

DaoK
Nov 20th, 2001, 06:58 PM
Why my code do not work, can someone copy and paste it and tell me why it do not work :(

crptcblade
Nov 20th, 2001, 07:38 PM
let me ask you this...are all the strings you are splitting this way in the same format?

Like:
First Name Last Name Number

?

:)

DaoK
Nov 21st, 2001, 05:49 PM
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.

Dillinger4
Nov 21st, 2001, 07:19 PM
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. :p


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
}
}