Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Use this:

words = text.split(" ");
String[] words = text.split(" ");

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top