• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parsing

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use java.util.StringTokenizer to break the string at the comma and then use it as u like
 
aliya sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention, fruit is a column in the table food
 
Saritha Penumudi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
aliya sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I hope changing to this will solve your problem.
 
aliya sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Shailesh,
i figured out the problem. Thanks a lot. And I really appreciate your help saritha.

Aliya
 
Shailesh Chandra
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congrats
 
Saritha Penumudi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Shailesh
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic