14 Replies Latest reply: Mar 26, 2009 11:42 AM by 807588 RSS

    Help with StringTokenizer

    807588
      I don't understand this and would like clarification on how to use an array of strings with this. here is my code

      The error is in this line
      StringTokenizer st = new StringTokenizer(names);
      (it wont accept the names array)
      import java.util.StringTokenizer;
      
      public class TestingStrings {
      
           public static void main(String[] args) {
                String[] names = {"Thoreau,Orwell,Tolstoy,Emerson"};
                
                StringTokenizer st = new StringTokenizer(names);
                
                while (st.hasMoreTokens()) {
                    System.out.println(st.nextToken());
                }
           }
      
      }
      Thanks :)

      Edited by: Gu3rr1lla on Mar 25, 2009 1:27 PM
        • 1. Re: Help with StringTokenizer
          camickr
          You can't.

          I don't even understand what you are trying to do. The names are already tokenized since they are separate entries in the array.
          • 2. Re: Help with StringTokenizer
            699554
            Gu3rr1lla wrote:
            The error is in this line
            StringTokenizer st = new StringTokenizer(names);
            (it wont accept the names array)
            What has made you believe that a StringTokenizer can accept an array of Strings as a parameter to its constructor. I suggest looking up the API for StringTokenizer before making assumptions about a class.

            Mel
            • 3. Re: Help with StringTokenizer
              807588
              Well i want to read each of the words so i can use them. I know there are a much simpler ways to do that but i have to use StringTokenizer for a question im doing regarding XML parsing.
              • 4. Re: Help with StringTokenizer
                807588
                Okay thanks i think i might have confused myself earlier.
                • 5. Re: Help with StringTokenizer
                  807588
                  I'll extend this question.

                  Earlier on i was doing something like this while reading an XML file:
                  while(in.hasNext());
                  
                  token = in.next();
                  
                  if(token.equals("<name>"){
                  token = in.next();
                  Object.setName(token);
                  }
                  So basically now i have to read the words from an array string in the same class and compare it with another array string. I want to read each word with StringTokenizer. Is something like this possible? Thanks

                  Edited by: Gu3rr1lla on Mar 25, 2009 1:45 PM

                  Edited by: Gu3rr1lla on Mar 25, 2009 1:45 PM
                  • 6. Re: Help with StringTokenizer
                    camickr
                    There is no need to use a tokenizer. The tokens are already defined in the array. So you just write a "for loop" to access each individual token.
                    • 7. Re: Help with StringTokenizer
                      807588
                      Also, you realize you only have one element in your String array? Did you mean to do this instead..
                      String[] names = {"Thoreau","Orwell","Tolstoy","Emerson"};
                      • 8. Re: Help with StringTokenizer
                        807588
                        oops yes thats what i meant to do. lol.

                        it still doesnt work like that either.
                        • 9. Re: Help with StringTokenizer
                          807588
                          Gu3rr1lla wrote:
                          it still doesnt work like that either.
                          ???
                          • 10. Re: Help with StringTokenizer
                            807588
                            You're just trying to iterate through the strings in your names array?
                            for (String name : names) {
                              System.out.println(name);
                            }
                            That would do the same thing that you were trying to do with the tokenizer. Except in this case since you're using an array its already 'tokenized' or broken into elements already for you.
                            • 11. Re: Help with StringTokenizer
                              807588
                              Hey guys thanks for the help but now i realise where my problem is..

                              im reading in an XML file

                              e.g.
                              <books>walden,nineteen eight four, war and peace</books>
                              and want to read in each book seperately and compare it with an array i have and if its in the array i will use it (send it to my favbooks)
                              if(token.equals("<books>")){
                                                 token = in.next();
                                                 String[] books = token; //Dont know what to do here
                                                 for(int i=0;i<token.length();i++){
                                                      if(books==favBooks[i]){
                                                       tempAuthor.addBook(books[i]);
                                                  }
                                             }
                                             
                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                              • 12. Re: Help with StringTokenizer
                                807588
                                got this working.

                                thanks guys for clearing some things up earlier. sometimes java doesnt stick first time i look at it.
                                • 13. Re: Help with StringTokenizer
                                  807588
                                  Gu3rr1lla wrote:
                                  Hey guys thanks for the help but now i realise where my problem is..

                                  im reading in an XML file

                                  e.g.
                                  <books>walden,nineteen eight four, war and peace</books>
                                  Who ever came up with that XML document definition needs his head examined! Why even use XML if you're not going to express the structure in the data.
                                  • 14. Re: Help with StringTokenizer
                                    807588
                                    Well i didnt want to give all the code because im working on an assignment for college and didnt want any lecturer thinking i was cheating if they found this :P

                                    Edited by: Gu3rr1lla on Mar 26, 2009 9:41 AM