Results 1 to 2 of 2
  1. #1
    viperlasson is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default String Tokenizer

    Alrighty, I'm making a program that will accept a couple commands on one line, seperated by white spaces. Here are some of the commands,

    Java Code:
    a   <setNumber>   <movieName>                    Add a movie to a set
    r   <setNumber>   <movieName>                    Remove a movie to a set
    m   <setNumber>   <movieName>                    Checks if a movie is in a set
    Right now im using sting tokenizer, and spliting up those 3 tokens and storing them into strings.

    But the problem I have is that a movie title can contain more than one token, so this is what I had to do...

    Java Code:
    			    if (st.hasMoreTokens()) //This is the command
    			    	s1 = st.nextToken();
    			    if (st.hasMoreTokens()) //This is the set number I later parse to an int
    			    	s2 = st.nextToken();
    			    if (st.hasMoreTokens()) //Here is where movie title starts
    			    	s3 = st.nextToken();
    			    if (st.hasMoreTokens())
    			    	s4 = st.nextToken();
    			    if (st.hasMoreTokens())
    			    	s5 = st.nextToken();
    			    if (st.hasMoreTokens())
    			    	s6 = st.nextToken();
    			    if (st.hasMoreTokens())
    			    	s7 = st.nextToken();
    			    if (st.hasMoreTokens())
    			    	s8 = st.nextToken();
    			    if (st.hasMoreTokens())
    			    	s9 = st.nextToken();
    It will store tokens into a string for a movie title up to 7 words long lol.... Is there an easier way to be doing this? This looks really messy/sloppy. Thanks.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Not great, but a little better?
    Java Code:
    if (st.hasMoreTokens()) //This is the command
      s1 = st.nextToken();
    if (st.hasMoreTokens()) //This is the set number I later parse to an int
      s2 = st.nextToken();
    while (st.hasMoreTokens()) //Here is where movie title starts
      s3 = s3 + " " + st.nextToken();
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. String Tokenizer
    By redasu in forum Advanced Java
    Replies: 4
    Last Post: 02-19-2010, 03:30 AM
  2. Manipulating String Tokenizer
    By Bomber_Will in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 11:54 PM
  3. string tokenizer
    By twinytwo in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 02:10 PM
  4. Problem with string tokenizer
    By twinytwo in forum AWT / Swing
    Replies: 2
    Last Post: 03-26-2009, 11:27 AM
  5. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •