Solved

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

Posted on 2003-11-25
10
295 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring AOP 6 85
maven project jars 7 53
difference between projection and projection list in hibernate 1 108
web application structure 18 77
Read about the ways of improving workplace communication.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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