• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can StringTokenizer ignore whitespace??

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to read and separate a comma separated list, but I only want the StringTokenizer to use the comma to separate the words and not whitespace. I set the code as:
String text = "black white,blue green"
StringTokenizer st = new StringTokenizer(myString, ",");
String[] list = new String[2];
int count = 0;
while (st.hasMoreTokens()){
list[count] = st.nextToken();
count++;
}
I want "black white" to go into list[0], and "blue green" to go into list[1], however the StringTokenizer still separates the list on white spaces as well as the commas, and I end up with an IndexOutOfBoundsException occuring.
Any suggestions???
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sair Legge:
I want to read and separate a comma separated list, but I only want the StringTokenizer to use the comma to separate the words and not whitespace. I set the code as:
String text = "black white,blue green"
StringTokenizer st = new StringTokenizer(myString, ",");
String[] list = new String[2];
int count = 0;
while (st.hasMoreTokens()){
list[count] = st.nextToken();
count++;
}
I want "black white" to go into list[0], and "blue green" to go into list[1], however the StringTokenizer still separates the list on white spaces as well as the commas, and I end up with an IndexOutOfBoundsException occuring.
Any suggestions???
- the below code works - you had a few typos
- notice that myString has been replaced by text
- and the System.out.println() to test the output
String text = "black white,blue green";
StringTokenizer st = new StringTokenizer(text, ",");
String[] list = new String[2];
int count = 0;
while (st.hasMoreTokens())
{
list[count] = st.nextToken();
System.out.println(list[count]);
count++;
}


 
reply
    Bookmark Topic Watch Topic
  • New Topic