jQuery in Action, 2nd edition*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "please clarify this." Watch "please clarify this." New topic
Author

please clarify this.

poornima sudharshan
Greenhorn

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 );
break;
}
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.
poornima.
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
Poornima,
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.


Thanks,<BR>Ravikiran<BR>rkchoppalli@yahoo.com
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
FWIW
I achieved this using
StringTokenizer stk = new StringTokenizer(string,"^");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please clarify this.