Results 1 to 11 of 11
  1. #1
    Bambi is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default SOLVED: Method that separates a string with a delimiter

    Hi,

    I have an assignment in which I have to create a method String[] explode that splits a string s according to the delimiter d, that returns an array of the 'words' between the delimiter. I am only allowed to use the string class's length, charAt, and substring methods.

    I know I can use a nested for loop but I don't know exactly how to use it. I'm not asking for anyone to write the code for me;I just need someone to explain to me how I would go about writing this. It would help me so much. I really want to be able to write this.




    Thanks either way,

    Bambi
    Last edited by Bambi; 01-27-2012 at 04:49 AM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Help Please! Method that separates a string with a delimiter

    iterate over the string (index=0....string.length) and check if charAt(index) is equal to the delimiter. if its the delimiter/seperator, call substring(0,index) and save the string into an array(or a dynamic list and at the end you call toArray)! save the index in a local variable for the next substring call / next found (substring(lastIndex,index)) ....and so on
    Last edited by eRaaaa; 01-26-2012 at 06:22 PM.

  3. #3
    Bambi is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help Please! Method that separates a string with a delimiter

    I'm sorry, but could you break that down to a bit more detail please?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: Help Please! Method that separates a string with a delimiter

    Do you know how to use each of the String methods you are supposed to use? If not, you need to write some simple program to experiment with each of them to see how they work and how to use them.

    To find the steps needed to solve this problem, start with a piece of paper and write down a sample string with some delimiters. How would you manually separate the words in the string. Suppose you need to tell some one else how to do the steps, what would you tell them?
    Something like:
    start at the left end,
    look at the next char
    is the character a delimiter?
    if not continue to the next char

    When you find a delimiter, then you need to tell them how to isolate and extract the word.

  5. #5
    Bambi is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help Please! Method that separates a string with a delimiter

    I've tried, but I just don't come up with any code from it. I guess this will be an assignment I fail. Sorry for wasting time.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: Help Please! Method that separates a string with a delimiter

    Do it in small steps. First you need to look at each letter.
    Post your code that looks at each letter in the String in a loop one at a time.

  7. #7
    Bambi is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help Please! Method that separates a string with a delimiter

    It's like I know how to use the methods that I need to individually, I just don't know how to use them and everything else I know to write this method.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: Help Please! Method that separates a string with a delimiter

    I know how to use the methods that I need to individually
    Then start simply and do it one step at a time. See my last post.

  9. #9
    Bambi is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help Please! Method that separates a string with a delimiter

    Thanks.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: Help Please! Method that separates a string with a delimiter

    If you don't try, you're not going to learn.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help Please! Method that separates a string with a delimiter

    Quote Originally Posted by Bambi View Post
    I've tried, but I just don't come up with any code from it. I guess this will be an assignment I fail. Sorry for wasting time.
    Don't give up so easily; suppose you find 'n' delimeters in your String. How long should your array be? A second pass over the String can use the substring( ... ) method to extract (explode) your String.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. identify the delimiter..
    By saidinu in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 05:16 PM
  2. Replies: 3
    Last Post: 01-03-2011, 03:48 PM
  3. Replies: 10
    Last Post: 12-07-2010, 12:29 PM
  4. How do you Use Backslash as a Delimiter?
    By Mobius-1 in forum New To Java
    Replies: 3
    Last Post: 12-14-2009, 12:39 AM
  5. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 10:50 PM

Posting Permissions

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