File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes please clarify this. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "please clarify this." Watch "please clarify this." New topic

please clarify this.

poornima sudharshan

Joined: May 18, 2001
Posts: 18
public class Testing {
public static void main( String args[] ) {
String s = "SA001^SFO^DEN^SpeedyAir^400^Sun^13:40^20m^50";
String[] s2 = new String[9];
int aIndex = s.indexOf('^');
int bIndex = 0;
int cIndex = -1;
for( int i = 0; i < 9; i++ )
if( bIndex == -1 )
s2[i] = s.substring( ++cIndex );
s2[i] = s.substring( ++cIndex, aIndex );
cIndex = aIndex;
bIndex = s.indexOf('^', aIndex );
aIndex = bIndex;
System.out.println( s2 );

The main purpose of this class is to convert the String object to an array of Strings.
But the compiler complains of String index out of Bonds exception.
please clarify this for me.
Doug Melzer
Ranch Hand

Joined: Mar 23, 2000
Posts: 51
You many want to consider using a StringTokenizer.
Ravikiran Choppalli
Ranch Hand

Joined: Nov 18, 2000
Posts: 49
Hope this helps !
StringTokenizer st = new StringTokenizer(s,"^\")
while (st.hasMoreTokens())
st.nextToken() ;
//store in the array
You can use int numberOfTokens = st.countTokens() to construct the array size
- Ravi.

Ravikiran Choppalli
Ranch Hand

Joined: Nov 18, 2000
Posts: 49

Correction !
StringTokenizer st = new StringTokenizer(s,"^\"") ;
- Ravi.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
I achieved this using
StringTokenizer stk = new StringTokenizer(string,"^");
I agree. Here's the link:
subject: please clarify this.
Similar Threads
Suggestions on fastest way to parse a String?
Problem with Email Program in TCP
String to String array
doubt in indexOf()
wants to trap email errors