Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2012, 01:16 PM   PM User | #1
bullet1
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bullet1 is an unknown quantity at this point
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
bullet1 is offline   Reply With Quote
Old 12-19-2012, 03:42 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-19-2012, 06:46 PM   PM User | #3
bullet1
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bullet1 is an unknown quantity at this point
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
bullet1 is offline   Reply With Quote
Old 12-19-2012, 07:02 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-22-2012, 02:56 PM   PM User | #5
bullet1
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bullet1 is an unknown quantity at this point
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 :/
bullet1 is offline   Reply With Quote
Old 12-22-2012, 03:08 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-29-2012, 12:40 AM   PM User | #7
rich1051414
New to the CF scene

 
Join Date: Dec 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
rich1051414 is an unknown quantity at this point
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("[; ]")
rich1051414 is offline   Reply With Quote
Old 12-29-2012, 08:58 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-29-2012, 10:47 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
java split

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:10 PM.


Advertisement
Log in to turn off these ads.