![]() |
Java split problem
Hi,
i'm a beginner in java coding and i have a small problem, i have a table contains a field "items", in this field, i have a lot of ID separated by ";", i have successfully extract all of the values from the field, but my question is how to get the value of the IDs one per one, i will need to do a verification after that (if (items[x] == something), but how i will be able to know the number of X if i use the split method ? thank's |
You don't, but it doesn't matter. You'll iterate them using a for loop (
for (int i = 0; i < items.length; ++i)), or use a for each syntax (for (String d : items)). If you use the for loop (not the foreach syntax), then you can simply pull the key based on the value of i.Since I would assume you either tokenized or used the string's split method, you can make your comparisons using the .equals() function on it. Don't compare strings with ==. If this data comes from something stored like a database, then you should review the schema as you have a normalization problem with the property holding a collection. |
Thank's for your reply, i did it but i doesn't work, here is my code :
Code:
if(infos.length == 0)return; |
That will always show a failure; you are comparing a string to an integer which is automatically false.
Cast id to a string or the string to an Integer and then check for equals. |
Code:
String items = _perso.getItemsIDSplitByChar(";"); // it's the function that take the value of the field and separate the IDS by ";" |
Before the if branch add this:
PHP Code:
|
Quote:
.split("[; ]") |
Quote:
.split(";"); and .split("[; ]"); would return the same results. |
In point of fact, using
.split("[; ]") could very well lead to the wrong results if there *ARE* spaces in the string that is split.Clearly if you had the string Code:
1 potato; 2 potato; 3 potato moreCode:
1 potato;). But you would get Code:
1Possibly more useful would be to split on ";\\s*" ??? That would get you Code:
1 potato; |
| All times are GMT +1. The time now is 01:49 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.