This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

please clarify this.  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You many want to consider using a StringTokenizer.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravikiran Choppalli
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Correction !
StringTokenizer st = new StringTokenizer(s,"^\"") ;
- Ravi.
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW
I achieved this using
StringTokenizer stk = new StringTokenizer(string,"^");
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!