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;
String items = _perso.getItemsIDSplitByChar(";"); // it's the function that take the value of the field and separate the IDS by ";"
String[] item = items.split(";");
int id = 141375;
for (int i = 0; i < item.length; i++)
{
if (item[i] != null && item[i].equals(id)) {
SocketManager.GAME_SEND_MESSAGE(_perso, "You have the item", Ancestra.COLOR_BLEU2);
break;
}
else
{
SocketManager.GAME_SEND_MESSAGE(_perso, "You don't have the item", Ancestra.COLOR_ERROR);
break;
}
It always show "You don't have the item" even if the ID exist in the field :S
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.
String items = _perso.getItemsIDSplitByChar(";"); // it's the function that take the value of the field and separate the IDS by ";"
String[] item = items.split(";");
String id = "141375";
for (int i = 0; i < item.length; i++)
{
if (item[i] != null && item[i].equals(id)) {
SocketManager.GAME_SEND_MESSAGE(_perso, "You have the item", Ancestra.COLOR_BLEU2);
break;
}
else
{
SocketManager.GAME_SEND_MESSAGE(_perso, "You don't have the item", Ancestra.COLOR_ERROR);
break;
}
String items = _perso.getItemsIDSplitByChar(";"); // it's the function that take the value of the field and separate the IDS by ";"
String[] item = items.split(";");
String id = "141375";
for (int i = 0; i < item.length; i++)
{
if (item[i] != null && item[i].equals(id)) {
SocketManager.GAME_SEND_MESSAGE(_perso, "You have the item", Ancestra.COLOR_BLEU2);
break;
}
else
{
SocketManager.GAME_SEND_MESSAGE(_perso, "You don't have the item", Ancestra.COLOR_ERROR);
break;
}
I doesn't work :/
Split uses regex, not literal strings, try this:
.split("[; ]")
Split uses regex, not literal strings, try this:
.split("[; ]")
Spit accepts a regex; however, a simple match of ";" is a valid regex as well. You don't need to block it into a character class to match the semi-colon when its the only criteria for your pattern match. There is no indication here that a space exists within the original string, so assuming that there is no space separation than both .split(";"); and .split("[; ]"); would return the same results.