| Author |
parsing
|
aliya sharma
Ranch Hand
Joined: Aug 11, 2004
Posts: 48
|
|
how do i parse comma seperated values?. For example: if a user enters in apple,orange in the single text field and submits the form, controller servlet should know 2 values are being sent from a text field so parse them and send them to the bean to query the database as: Select * from food where fruit in ('apple','orange'). Hope i made myself clear on the problem. Any ideas or relative code would be greatly appreciated. Thanks for your help. Regards, Aliya
|
 |
Anupreet Arora
Ranch Hand
Joined: Jun 17, 2003
Posts: 81
|
|
use java.util.StringTokenizer to break the string at the comma and then use it as u like
|
 |
aliya sharma
Ranch Hand
Joined: Aug 11, 2004
Posts: 48
|
|
Thanks for your response, Anupreet. I was going to use StringTokenizer, but i don't know how to get the seperated value. For example:, lets say user enters Apples, oranges(it could be more). THis is what i know how to do in my controller servlet. What do i do after that to get jst Apples oranges. I know how to print them. but i need to pass these values from my servlet to the bean(number of values could vary)? Aliya
|
 |
Saritha Penumudi
Ranch Hand
Joined: Aug 18, 2003
Posts: 146
|
|
All you could do is, let say u have a method called getFruits() and setFruits() methods in your bean. The signatures f your methods would be In your servlet, StringTokenize your string with comma seperator and say if you don't want to create ArrayList with default size. you can make use of countTokens() method of StringTokenizer to get the size of the fruits. Hope this helps you Saritha
|
 |
aliya sharma
Ranch Hand
Joined: Aug 11, 2004
Posts: 48
|
|
Hello Saritha, THanks for your response. i want to use the result of StringTokenizer in my SQL query in my java bean. let's say i have a table by the name of food, with fruit (with values APPLES, ORANGES,Bananas as its values). So, if the user enters in Apples, Oranges. My StringTOkenizer would seperate the values and send it to my bean(i do have set method in my bean). Now,In my bean, i want to use this query: as you can see the parameters in IN part of the query could be more or less depending on the user's input. I'm really confused in how to achieve this. I would really appreciate any help or suggestions. Thanks a lot for your time and help Aliya
|
 |
aliya sharma
Ranch Hand
Joined: Aug 11, 2004
Posts: 48
|
|
|
I forgot to mention, fruit is a column in the table food
|
 |
Saritha Penumudi
Ranch Hand
Joined: Aug 18, 2003
Posts: 146
|
|
That would be continuation to the above code. After you set the fruits Arraylist to the bean, call getFruits() which takes ArrayList as parameter on your bean. FYI: You can make use of Transfer objects that carry information to your bean. The code in getFruitDetails() method of your bean would like this. \ are used to escape single quote, and brackets I did not test this program. Hope this will work. Let me know if you have more questions. Saritha [ October 26, 2004: Message edited by: Saritha ventrapragada ]
|
 |
Shailesh Chandra
Ranch Hand
Joined: Aug 13, 2004
Posts: 1076
|
|
I would like to add one more thing that to be taken care whenver using StringTokenizer. StringTokenizer returns next token whenever it gets a null token eg: the case whet it get 2 comma together in String or even string starts with comma... There is one more way to break string in java As of JDK 1.4 String class has a method called split to split strings and an array is returned.
|
Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
|
 |
aliya sharma
Ranch Hand
Joined: Aug 11, 2004
Posts: 48
|
|
Hello Saritha and Shailesh, Thanks a lot for your help. Saritha, i'm following your example. But when i try to execute my query, its giving me a type mismatch compiler error: cannot convert from string to string buffer this is my code: Any ideas?. Again, i really appreciate your help. Aliya
|
 |
Shailesh Chandra
Ranch Hand
Joined: Aug 13, 2004
Posts: 1076
|
|
I hope changing to this will solve your problem.
|
 |
aliya sharma
Ranch Hand
Joined: Aug 11, 2004
Posts: 48
|
|
Hey Shailesh, i figured out the problem. Thanks a lot. And I really appreciate your help saritha. Aliya
|
 |
Shailesh Chandra
Ranch Hand
Joined: Aug 13, 2004
Posts: 1076
|
|
Congrats
|
 |
Saritha Penumudi
Ranch Hand
Joined: Aug 18, 2003
Posts: 146
|
|
Your welcome Aliya. Shailesh I have one question for you. You were suggesting using split function of String. Can you compare String split function and StringTokenizer w.r.t performance. Which one is better? Thank you Saritha
|
 |
Shailesh Chandra
Ranch Hand
Joined: Aug 13, 2004
Posts: 1076
|
|
Saritha , As far as case of performance I can not say anything as I am not aware of internal implementation. but any way i feel String.split is better. I read at one Web site that "The J2SE API Specification now documents StringTokenizer as a legacy class and discourages its use, but doesn't go so far as to deprecate it." Advantage of split is already mentioned in one of my post. So using split avoids overhead of fixing input String. [ October 26, 2004: Message edited by: Shailesh Chandra ]
|
 |
Saritha Penumudi
Ranch Hand
Joined: Aug 18, 2003
Posts: 146
|
|
|
Thank you Shailesh
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Also look at StringTokenizer option to return the dlimiters. Then it might return If all this is confusing, just make sure you have test cases that simulate truly evil users. If you're into unit testing (see JUnit.org) you'd have good incentive to put the parsing and SQL building into a separate class that you can test without running the whole server and checking database results. Have fun!
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: parsing
|
|
|