Solved

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

Posted on 2003-11-25
10
293 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now