Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java StringTokenizer question.

  • 16-05-2003 12:24pm
    #1
    Closed Accounts Posts: 7,230 ✭✭✭


    This may be a very silly question, but i've been awake all night studying for a math exam and I can't find a solution. Anyway...


    System.out.print("\nEnter string: ");
    s=input.readLine();
    StringTokenizer st = new StringTokenizer(s);
    while(st.hasMoreTokens()) {
    st.nextToken();
    count++;
    }

    Ok this counts how many Tokens are in the sentence the user enters. That's all well and good. What i want to do is put the first token into the first element of an array called sarr[], the second token into the second element of the array, and so on for each token in the sentence no matter how long the sentence is.

    I tried using a for loop iterating to s.length(); but that counts the characters in the string and iterates that number of times. There's no length() or count() for the StringTokenizer class to tell me how many tokens are in the string! Any ideas? Have i lost the plot?


Comments

  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    What about the countTokens method?

    Zab.


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    use a vector

    Vector myVector = new Vector();

    System.out.print("\nEnter string: ");
    s=input.readLine();
    StringTokenizer st = new StringTokenizer(s);
    while(st.hasMoreTokens()) {

    myVector.add(st.nextToken());

    //st.nextToken();
    count++;
    }

    then at the end of this all your tokens are held in a vector (which is like an expandable array)

    to rectrieve them :

    for (int i=0; i < count; i++ ) {
    st = (StringTokenizer) myVector.get(i);

    do something with st here
    }

    Edit *

    to put it into an array
    StringTokeniser myArray[] = new StringTokeniser[myVector.size()];

    for (int i = 0; i < myArray.length; i++ ) {
    myArray = (StringTokeniser) myVector.get(i);
    }


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Vectors suck tbh. (No offence hussey)

    The countTokens() method will do all the work for you sjones (as Zab said).

    The following piece of code should do it for you:
    System.out.print("\nEnter string: ");
    String s = input.readLine();
    StringTokenizer st = new StringTokenizer(s);
    String[] arr = new String[st.countTokens];
    int i = 0;
    for(String a = st.nextToken(); st.hasMoreTokens(); a = st.nextToken()) {
    arr[i] = a;
    i++
    }
    

    If you get arrayIndexOutOfBoundsException, replace the guard in the for loop with the usual stuff for looping through arr instead of st.

    :)


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    Unless you need the synchronization, use an ArrayList over a Vector. Also, both of these classes have toArray methods to convert into an array. And note that the nextToken method returns a String rather than a StringTokenizer.

    Using an array instead of one of the collection classes (above) would be more efficient, but efficiency may not be required? On the other had, if you look at seamus' code, it doesn't make life that much more difficult.

    Zab.


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Thanks very much guys. Sorry about the late reply. Been studying!


  • Advertisement
Advertisement