2

I am making a program which would have the user enter a sentence and following that, the app would break the String into sub-strings where spaces are what break the original string up.

import java.util.StringTokenizer;


    public class whitespace {

    public static void main(String[] args) {

    String text = "supervisors signature tom hanks";
    int tokenCount; //number of words
    int idx=0; // index
    String words[]=new String [500]; // space for words


     StringTokenizer st=new StringTokenizer(text); // split text into segements
     tokenCount=st.countTokens(); 
     while (st.hasMoreTokens()) // is there stuff to get?
     {
         words[idx]=st.nextToken();
         idx++;
     }
}

I have this code thus far and while it works fine as a regular Java program, the while loop seems to cause the app to go into an infinite loop. Any ideas?

TomSelleck
  • 6,706
  • 22
  • 82
  • 151
  • 1
    You should replace your String array with an string arraylist. Creating an array of 500 strings is not very efficient. Arraylists on the other hand are dynamic and can grow to fit your needs. – Andreas Mar 06 '12 at 12:33

5 Answers5

5

I think that you can use the String.split method for this:

String text = "supervisors signature tom hanks";
String[] tokens = text.split("\\s+");
for (String str : tokens)
{
    //Do what you need with your tokens here.
}

The regex will split the text into sentences wherever it encounters one or more space characters.

According to this page, the StringTokenizer has been replaced with String.split.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Using a regex for such a simple split seems a bit of an overkill. – Aidiakapi Mar 06 '12 at 12:33
  • @Aidiakapi: If that is the case, the OP can simply replace it with `text.split(" ");`. – npinti Mar 06 '12 at 12:39
  • Yes he could and should, but this answer doesn't tell him to. – Aidiakapi Mar 06 '12 at 12:43
  • Using regex is a better practice because the argument to `String.split(String)` will be evaluated as regex in `Pattern.compile(String)` anyway, and will prevent empty array elements from being returned, reducing the size of the returned array, and eliminating check for empty strings in code. – Alex Mar 06 '12 at 14:51
4

Use this:

words = text.split(" ");
Android Killer
  • 18,174
  • 13
  • 67
  • 90
1
String[] words = text.split(" ");
mihail
  • 2,173
  • 19
  • 31
1

Use Apache StrTokenizer

StrTokenizer strTok = new StrTokenizer(text);
String[] strList = strTok.getTokenArray();

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/text/StrTokenizer.html

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
0
StringTokenizer sta=new StringTokenizer(text); // split text into segements
     String[] words= new String[100];int idx=0;
     while (sta.hasMoreTokens()) // is there stuff to get?
     {
         words[idx]=sta.nextToken();
         System.out.println(words[idx]);
         idx++;
     }

This is what I copied your code and executed by changing little and it worked fine.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125