Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StringTokenizer 2

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
0
0
US
Hi All,

Trying to parse a string using StringTokenizer. The line I'm attempting to parse acctNames.elementAt(i) is:
AA%20Test%20Inc%20%5BAAB%5D%20%5B1111111111%5D

Here is my code for parsing:
Code:
...
StringTokenizer sTokenizer = new StringTokenizer((String)acctNames.elementAt(i), "%5B");
tempToken = sTokenizer.nextToken();
...

I'm trying to parse at %5B and return in tempToken AAB. But what is returning is AA. Can someone help me out please.

TIA,
Tim
 
The delimeters in a StringTokenizer are not a literal string of "%5B" but mean "delimit my String using "%", "5" or "B" .

Look at the String method split() which will split/tokenize using a regular expression.

Or you could run that string through a URLDecoder to simplify it (it is a http sent string is it not ?)

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks All,

How would I use the split() with (String)acctNames.elementAt(i);. Thanks.
 
Thanks for the help Diancecht.

String [] weAreYourTokens = (String)acctNames.elementAt(i).split("%5B");

I tried your suggestion and getting the following error.

The method split(String) is undefined for the type Object.
 
Try :

String [] weAreYourTokens = ((String)acctNames.elementAt(i)).split("%5B");

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for the help sedj and all. Forgive me but I'm still having trouble with .split(). I entered
Code:
String [] weAreYourTokens = ((String)acctNames.elementAt(i)).split("%5B");

and I'm getting the error "The method split(String) is undefined for the type String". I added import java.lang.String and still have the same issue. What am I missing?

Any help would be appreciated.

TIA,
Tim
 
As an aside, you don't need to import any of the java.lang classes.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
It compiles for me. Have you got it exactly as sedj wrote it?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Curious. Maybe you'd better post your code, if it's not too long.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Here is the code...
Code:
package com.pnc.tms.ach.reports;

import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pnc.tms.ach.ejb.ACHReportEJB;
import com.pnc.tms.ach.ejb.ACHReportEJBHome;
import com.pnc.tms.ach.ejb.ACHReportEJBKey;
import com.pnc.tms.ach.ejb.ACHSessConnectEJB;
import com.pnc.tms.ach.ejb.ACHSessConnectEJBHome;
import com.pnc.tms.ir.constants.IREJBConstants;
import com.pnc.tms.isc.inforeports.Utils;
import com.pnc.tms.isc.system.EJBFactory;
import com.pnc.tms.isc.system.ISCException;

/**
 * @author PP15895
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public final class EJBACHReportsML implements IISCACHReports {
	private static Log log = LogFactory.getLog(EJBACHReportsML.class);
	
	ACHReportEJB custReport = null;
	ACHReportEJBHome custRepHome = null;
	ACHReportEJBKey custRptKey = null;
	ACHSessConnectEJBHome sessConnEJBHome = null;
	ACHSessConnectEJB sessConnEJB = null;
	
	String tempShortName = null;
	String tempToken = null;

	/**
	 * Constructor for EJBACHReportML.
	 */
	public EJBACHReportsML() {
		super();
	}
	public void ReportsProcessing(ACHRptObjects rptObjs) throws ISCException {
		if(log.isDebugEnabled()) 
			log.debug("OAE4403" + ": " + "EJBACHReportsML.ReportsProcessing() entered.");
		
		Integer seqNum;
		//int seqNum = 10;
		

		boolean error = false;
		
		try {
  			
			try {
				//sessConnEJB = ((ACHSessConnectEJBHome)EJBFactory.getEJBHome(IREJBConstants.ACHSessConnectEJBHome_ref)).create();
				//Getting seqNum from SRS_ACH_ORIG_TEMP table...
				//If table is empty, seqNum defaults to 1.  Otherwise, max seqNum
				//should be returned.
				//seqNum = sessConnEJB.getSeqNum();
				
				//if (seqNum != null) {
				//	rptObjs.setSeqNum(seqNum);
				//}
				
				//System.out.println("seqNum is ... " + rptObjs.getSeqNum());
				
				Vector acctNames = Utils.parseStringToVector(rptObjs.getSelAccounts(), ",");
				
				for(int i = 0;i < acctNames.size(); i++) {
					//System.out.println("Names is ... " + acctNames.elementAt(i));
					//Parsing each short name from list for processing to SRS_ACH_ORIG_TEMP table....
					String [] weAreYourTokens = ((String)acctNames.elementAt(i)).split("%5B");
					//String x = (String)acctNames.elementAt(i);
					//Locating first occurrance of [ or %5B...
					//int y = x.indexOf("%5B",0);
					//Locating first occurrance of ] or %5D...
					//int z = x.indexOf("%5D",0); 
					//Removing %5B from x (y+3) and obtaining shortname...
					//tempShortName = x.substring((y+3),z);
										
					System.out.println("******ShortName is ... " + tempShortName);
					
					rptObjs.setShortName(tempShortName);
			
					custRepHome = (ACHReportEJBHome)EJBFactory.getEJBHome(IREJBConstants.ACHReportEJBHome_ref);
					  custReport =  custRepHome.create(rptObjs.getSeqNum(), rptObjs.getCompanyID(), rptObjs.getShortName(), 
					  rptObjs.getReportID(), rptObjs.getRelDateFrom(), rptObjs.getRelDateTo(), rptObjs.getEffDateFrom(), 
					  rptObjs.getEffDateTo(), rptObjs.getAmtFrom(), rptObjs.getAmtTo(), rptObjs.getRecrAcctNbr(), rptObjs.getIndivID());
				}				
			}  catch(Exception e) {
				error = true;
				String errorMsg = "EJBACHReportsML.ReportsProcessing() Exception caught: " + e.getMessage();
				log.error("OAX4403" + ": " + errorMsg, e);
				throw new ISCException("OAX4403");
			}
		} finally {
			custReport = null;
			custRepHome = null;
			sessConnEJBHome = null;
			sessConnEJB = null;
			if(!error)
				if(log.isDebugEnabled())
					log.debug("OAE4403" + ": " + "EJBACHReportsML.ReportsProcessing() exited.");			  				 
			}
	}
}
 
Looks right to me. You're definitely using jdk1.4? A later one like 1.4.2_06? Do a java -version to check if you're not sure.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I think I found the problem. I'm using java ver 1.3.1. Thought I was using 1.4...
 
..ahhhh. That'll be it then. :)

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Then you lose the benefits of split() method and you need to do it in the traditional way.

You'll need to use the indexOf and substring methods from String class.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top