Solved

URGENT!!!-how to split id (|)

Posted on 2003-11-25
10
300 Views
Last Modified: 2012-05-04
hi,

 I have a delete.jsp that receives ids such as user1|user2|user3

String[] id = request.getParameterValues("id") ;

    for(int i=0; i < id.length; i++)

    {

out.println(id[i]);

      String Query = "delete FROM volunteer where username='"+id[i]+"'";
      int status= statement.executeUpdate(Query);



 

  if(status != 0)
  {
   out.println("Entry successfully deleted.");
  }
 
  else
  {
  out.println("Deletion not successful");
  }

}

}


My problem is id[0] is user1|user2|user3


i need to split it into

id[0]=user1
id[1]=user2
id[2]=user3 and etc

i mean discard | and put into array.


Pls help , i need it urgently
0
Comment
Question by:Jasbir21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:vk33
ID: 9817693
Hi!

It's quite simple when using StringTokenizer:

import java.util.*;
...
String id = request.getParameter("id");
StringTokenizer parser = new StringTokenizer(id,"|");
while (parser.hasMoreTokens()) {
   String query = "DELETE FROM volunteer WHERE username = '" + parser.nextToken() + "'";
   ...
}

Good luck!
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 100 total points
ID: 9817700
well first I am not sure why you are getting id[0] as user1|user2 format but.. this is what you can do..

StringTokenizer st = new StringTokenizer(id[0],"|");
String[] ar = new String[st.countTokens()];
int i=0;
while(st.hasMoreTokens())
{
ar[i]=st.nextToken();
i++
}
This will put all the stuff in String array ar.
Hope this helps
Regards
0
 
LVL 2

Expert Comment

by:DidierD
ID: 9817807
You can use StringTokenizer for this.

Something like this

String ids="user1|user2|user3";
StringTokenizer tokenizer = new StringTokenizer(ids,"|");

String[] ids = new String[tokenizer.countTokens()];
int i = 0;
for (int i = 0; i< tokenizer.countTokens(); i++)
  ids[i] = tokenizer.nextToken();
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:DidierD
ID: 9817816
:(( too late again
0
 
LVL 92

Expert Comment

by:objects
ID: 9822759
String names = ids.split("|");
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9822947
gonna work only with JDK1.4...............
thats why I rely more on tokenizer
0
 
LVL 92

Expert Comment

by:objects
ID: 9822950
I realise that, just offering it as an option if Jasbir21 is running 1.4.
0
 
LVL 4

Expert Comment

by:vk33
ID: 9823018
just a comment:

String[] names = ids.split("|");

array is returned... :)

Regards!
0
 
LVL 92

Expert Comment

by:objects
ID: 9823027
> array is returned... :)

Oops :-)
0
 

Author Comment

by:Jasbir21
ID: 9831041
I am very sorry for the late response, i got stuck in another question, that i couldn't start with this until that problem was not solved.the program did not work , now it does.

  Very very sorry..

Thanks you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Changing a few Outlook Options can help keep you organized!
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question