String Tokenizer in Java doesn’t work

Asked By 10 points N/A Posted on -
qa-featured

I created a java program. For the I want to split sentence to different words and put them into a array.

I used following two codes for that. But, it doesn't work.

Help me for solve this. Thank you!

String[] result = "John Steve Smith".split(s);

     for (int x=0; x<result.length; x++)
         System.out.println(result[x]);
 
 
   StringTokenizer st = new StringTokenizer("John Steve Smith");
     while (st.hasTokens()) {
         System.out.println(st.next());
     }
SHARE
Answered By 0 points N/A #84646

String Tokenizer in Java doesn’t work

qa-featured

Your First Code:

String[] result = "John Steve Smith".split(s);
                                for (int x=0; x<result.length; x++)
                                      System.out.println(result[x]);
 
In your first code, the system will give you an error "cannot find variable is" this is because the s is read by the program as a variable which is not being initialized. 
 
You Code Should be:
 
                   String[] result = "John Steve Smith".split("\s");
                                for (int x=0; x<result.length; x++)
                               {
                                      System.out.println(result[x]);
                                }
 
Since you're using .split method, you should not forget that there is a delimiter for this method and that delimiter is the (\s)
 
For your second code:
 
             StringTokenizer st = new StringTokenizer("John Steve Smith");
                         while (st.hasTokens()) 
                         {
                              System.out.println(st.next());
                         }
 
Compiling this code of yours will give you an error cannot find symbol method next() and cannot find symbol method has Tokens()
 
this is because you wrote st.hasTokens() instead of st.hasMoreTokens() and st.next() rather than st.nextToken()
 
Your code should be:
 
StringTokenizer st = new StringTokenizer("John Steve Smith");
                         while (st.hasMoreTokens()) 
                         {
                              System.out.println(st.nextToken());
                         }
 
Now, for storing each word in our array, we need to have an array index reference. This will be our basis to where will the program will store the next token or word in our array since accessing the data and storing data in an array is based on an index / index.
 
Our code then will be:
 
String[] str=new String[3];
     int index=0;—- array index reference
     
           StringTokenizer st = new StringTokenizer("John Steve Smith");
           
                               While (St. hasMoreTokens ()) 
 
                                    {
                                    str[index]=(st.nextToken());
                                    index++;—- we need to increment out array reference to tell the program to where the next available Slot is
     }
     
     System.out.println(str[0]);
     System.out.println(str[1]);
     System.out.println(str[2]);
 
Hope I helped you. Thank you and good luck.

Related Questions