Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 :/

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bullet1 View Post
    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("[; ]")

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by rich1051414 View Post
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •