CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Java split problem (http://www.codingforums.com/showthread.php?t=284407)

bullet1 12-19-2012 01:16 PM

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

Fou-Lu 12-19-2012 03:42 PM

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.

bullet1 12-19-2012 06:46 PM

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

Fou-Lu 12-19-2012 07:02 PM

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.

bullet1 12-22-2012 02:56 PM

Code:

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 :/

Fou-Lu 12-22-2012 03:08 PM

Before the if branch add this:
PHP Code:

system.out.println("Compare: " id " to " item[i] + " = " id.compareTo(item[i])); 

And post what that results in.

rich1051414 12-29-2012 12:40 AM

Quote:

Originally Posted by bullet1 (Post 1301692)
Code:

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("[; ]")

Fou-Lu 12-29-2012 08:58 PM

Quote:

Originally Posted by rich1051414 (Post 1302819)
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.

Old Pedant 12-29-2012 10:47 PM

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 more
You would get the array
Code:

1 potato;
 2 potato;
 3 potato more

if you simply split on ";" (notice the spaces at the beginning of the 2nd and 3rd elements.
).

But you would get
Code:

1
potato
[empty string]
2
potato
[empty string]
3
potato
more

if you split on "[; ]"

Possibly more useful would be to split on ";\\s*" ???

That would get you
Code:

1 potato;
2 potato;
3 potato more

getting rid of those leading spaces.


All times are GMT +1. The time now is 01:49 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.