token separation in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sang
    New Member
    • Sep 2006
    • 83

    #1

    token separation in array

    I want to split the String using array. i am do it on the simple method.

    But i want change the below code by using array.

    class example1
    {
    public static void main(String arg[]) {

    StringTokenizer st = new StringTokenizer ("this is a test");
    while (st.hasMoreToke ns()) {
    String key=st.nextToke n();
    System.out.prin tln(key);
    }
    }
    }
    the out put is like this

    this
    is
    a
    test

    so pls do the needful

    Thanks
    Sang
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sang
    I want to split the String using array. i am do it on the simple method.

    But i want change the below code by using array.

    class example1
    {
    public static void main(String arg[]) {

    StringTokenizer st = new StringTokenizer ("this is a test");
    while (st.hasMoreToke ns()) {
    String key=st.nextToke n();
    System.out.prin tln(key);
    }
    }
    }
    the out put is like this

    this
    is
    a
    test

    so pls do the needful

    Thanks
    Sang
    Unfortunately you are not being very clear on what you want to do.
    The following might help:

    To convert a string to an array of characters, do string.toCharAr ray();

    The best string splitter method is string.split(re gexp); http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#su m


    The StringTokenizer should now be avoided. It is now one of those older classes which sun may soon deprecate.

    Comment

    • joseluc
      New Member
      • Sep 2006
      • 1

      #3
      Hi, you forgote the second StringTokenizer parameter.

      import java.util.Strin gTokenizer;

      public class TokenizerTest {
      public static void main(String[] args) {

      StringTokenizer st = new StringTokenizer ("this is a test"," ");
      while (st.hasMoreToke ns()) {
      String key=st.nextToke n();
      System.out.prin tln(key);
      }
      }
      }

      Comment

      • abadi
        New Member
        • Aug 2006
        • 5

        #4
        I hop this is what you want

        class Test {
        public static void main(String[] args) {
        ArrayList tokensArray = new ArrayList();
        StringTokenizer st = new StringTokenizer ("this is a test");
        while (st.hasMoreToke ns()) {
        tokensArray.add (st.nextToken() );
        }
        }
        }

        Comment

        Working...