Link to home
Start Free TrialLog in
Avatar of jhughes4
jhughes4

asked on

Simple question

Can someone provide some sample code on how I can parse the following message in JDK 1.4?

User name : Mike User email : someemail.com User phone :      719443453 User address : some address User city :      LA

I want the result to be:

Mike
someemail.com
719443453
some address
LA

thanks in advance.
Avatar of Manikandan Thiagarajan
Manikandan Thiagarajan
Flag of India image

use split method
Avatar of geekprog
geekprog


public static final String DELIMITER =  ":";
public static final String CONTACT =  ":";

String strDelimiter = (String)pRequest.getParameter(DELIMITER);
String strList = (String)pRequest.getParameter(CONTACT );


      StringTokenizer strTokenizer = new StringTokenizer(strList, strDelimiter);
      Vector vector = new Vector(10);

      while (strTokenizer.hasMoreTokens()) {
            vector.add(strTokenizer.nextToken().trim());
      }

        for(int i = 1; i <= vector.size(); i = i+2 )        // Here i am incrementing +2, to display only the Values you said
        {
               System.out.println( vector.elementAt(i));
        }
Avatar of jhughes4

ASKER

Thanks for your response, but I haven't coded in Java in a long time & I don't have time to go through some trial and error.  I have a Siebel developer who needs a sample Java code that demonstrates how Java can parse a message so that he can replicate that feature within Siebel.  So can you provide a sample?  Thanks again.
small correction in my above code

REPLACE

public static final String CONTACT =  ":";

WITH

public static final String CONTACT =  "Your Text Goes Here";   // Generally this should be read from the file


Received the following error:
java:9: cannot find symbol
symbol  : variable pRequest
location: class emailParse
String strDelimiter = (String)pRequest.getParameter(DELIMITER);
cannot find symbol
symbol  : variable pRequest
location: class emailParse
String strList = (String)pRequest.getParameter(CONTACT );

thanks
That was basically a pseudo code, anyway below is the complete code that you can run

import java.lang.*;
import java.util.*;

public class Sample
{
      public static final String DELIMITER =  " ";
      public static final String CONTACT =  "User name : Mike User email : someemail.com User phone : 719443453 User address : some address User city : LA";

      public static void main(String[] args)
      {
            StringTokenizer strTokenizer = new StringTokenizer(CONTACT,DELIMITER);
            Vector vector = new Vector(30);

            while(strTokenizer.hasMoreTokens()) {
                  vector.add(strTokenizer.nextToken().trim());
          }
      
        for(int i = 1; i <= vector.size(); i++ )
        {
                  String s = (String)vector.elementAt(i-1);
                  if(s.equals(":"))
                        System.out.println(vector.elementAt(i));
        }
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Manikandan Thiagarajan
Manikandan Thiagarajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial