Why is the problem? String Manipulation. Help Please.

Ok I have two while loops that run with two different String arrays. The problem that I have is that I have a String LeadPI that looks like this: "Dr. Ayusman Sen, Pennsylvania State University".
I would like to separate the String where the first half "Dr. Ayusman Sen" = String investigator; and "Pennsylvania State University" = String institution;
basically separate the string into two separate String variables.
can I do this in 1 while loop, and why is my output not functioning correctly?
Lead PI: Dr. Ayusman Sen, Pennsylvania State University //*** String to manipulate***//
// what the output should look like.
Institution: Pennsylvania State University //*** this should be institution ***//
Investigator: Dr. Ayusman Sen //*** this should be investigator ***//
// This is the current output!
Institution: Dr. Ayusman Sen
Institution: Pennsylvania State University //*** this should be institution ***//
Investigator: Dr. Ayusman Sen //*** this should be investigator. ***//
Investigator: Pennsylvania State University
Investigator: Dr. Ayusman Sen
Investigator: Pennsylvania State University            
             //********************** institution **************************//
            int count= 0;
            char separator =',';
            int index =0;
            do
               ++count;
               ++index;
               index = LeadPI.indexOf(separator, index);
            while (index != -1);                       
            String[] insti = new String[count];
            index=0;
            int endIndex = 0;
            for(int i = 0; i < count; i++)
                endIndex = LeadPI.indexOf(separator, index);
                if(endIndex == -1)
                    insti= LeadPI.substring(index);
else
insti[i] = LeadPI.substring(index, endIndex);
index = endIndex + 1;
for(int i = 0; i < insti.length; i++)
System.out.println("\tInvestigator: "+insti[i]);
//********************** investigator **************************//
index =0;
do
++count;
++index;
index = LeadPI.indexOf(separator, index);
while (index != -1);
String[] investi = new String[count];
index=0;
endIndex = 0;
for(int i = 0; i < count; i++)
endIndex = LeadPI.indexOf(separator, index);
if(endIndex == -1)
investi[i]= LeadPI.substring(index);
else
investi[i] = LeadPI.substring(index, endIndex);
index = endIndex + 1;
for(int i = 0; i < investi.length; i++)
System.out.println("\tInstitution: "+investi[i]);

Cheers!
One of the problems with the String's split method is that it will nto handle the spaces properly. You will still need to trim it.
In the class, below, I have coded for you two methods that will do what you are asking for.
The first method (split) uses the String's split method, but then has to go through and trim down the strings to remove the space after the comma.
The second method, tokenize, does the same thing, but uses a string tokenizer.
Both work - I was just trying to illustrate both methods. The tokenizer method is about three times faster than the split method (in my tests) - but the split method is a little cleaner (IMHO) - but we're talking sub-millisecond differences in one execution.
You would get the name from the [0] index of the returned String array and the institution from the [1] index, like so:
System.out.println( "Name: " + info[ 0 ] );
System.out.println( "Institution: " + info[ 1 ] );There are some issues with this code... if there is a comma in the institution name or the person's name, if messes things up - if, for example, the persons' name is "Paul Leska, Jr." - the routine splits the string in between Leska and Jr.
The same thing happens in the institution name - if, for example, the name of the place is "University of Minnesota, Duluth" - we tokenize or split between Minnesota and Duluth.
You'll either need to (1) watch your data, (2) make these routines smarter or (3) Change your input to already be split (from the source, perhaps).
Here's the code:
import java.util.StringTokenizer;
public class Splitter {
  public static String[] split( String s, char c ) {
    String[] chopped = s.split(String.valueOf( c ));
    for( int idx = 0; idx < chopped.length; idx++ ) {
      chopped[ idx ] = chopped[ idx ].trim();
    return chopped;
  public static String[] tokenize( String s, char c ) {
    StringTokenizer st = new StringTokenizer( s, String.valueOf( c ) );
    int count = st.countTokens();
    String[] chopped = new String[ count ];
    for( int idx = 0; idx < count; idx++ ) {
      chopped[ idx ] = st.nextToken().trim();
    return chopped;
  public static void main( String[] args ) {
    String[] info = tokenize( "Michael A. Riecken, Jumping Mouse Software", ',' );
    for( int idx = 0; idx < info.length; idx++ ) {
      System.out.println( info[idx] );
    info = split( "Michael A. Riecken, Jumping Mouse Software", ',' );
    for( int idx = 0; idx < info.length; idx++ ) {
      System.out.println( info[idx] );
}

Similar Messages

Maybe you are looking for

  • Imac as a monitor for a MacBook Pro

    I would like to use my 6 year old Imac as a monitor for my 1 yr old MacBook Pro.  Any suggestions as to how to hook this up?

  • PR release date report

    Hi, I want to get the report in which i can get exact release date of PR. In ME5A  / ME5j release date is not exact date at which the PR was released. Regards Kalpesh

  • Oracle 9i patches for linux 4.0 Advance server

    how do i download the required patches of oracle 9i for linux 4.0 Advanced server. I am 9i oracle certified professional. Currently I am not working. how it is possible for me to dowload pathces through www.metalink.oracle.com or is there any other w

  • Uninstall SAP BPC 7.0

    Hi all, I have problem uninstalling the SAP BPC 7.0 on Microsoft Platform. When I uninstall through Windows Add/Remove Component, the wizard stuck at the Removing Sample OLAP database? Any idea what am I missing out here? Is there a uninstall guide I

  • Camera zoom problem

    i havent had any problems with my camera up until now. i have had the Curve 8310 for 3  months and now all of a sudden the camera wont zoom. can anyone help me out with that. Solved! Go to Solution.