Results 1 to 11 of 11
- 01-26-2012, 06:15 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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,
BambiLast edited by Bambi; 01-27-2012 at 04:49 AM.
- 01-26-2012, 06:20 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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.
- 01-26-2012, 06:40 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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?
- 01-26-2012, 07:10 PM #4
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.
- 01-26-2012, 07:29 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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.
- 01-26-2012, 07:39 PM #6
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.
- 01-26-2012, 07:39 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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.
- 01-26-2012, 07:40 PM #8
Re: Help Please! Method that separates a string with a delimiter
Then start simply and do it one step at a time. See my last post.I know how to use the methods that I need to individually
- 01-26-2012, 07:53 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
Re: Help Please! Method that separates a string with a delimiter
Thanks.
- 01-26-2012, 07:59 PM #10
Re: Help Please! Method that separates a string with a delimiter
If you don't try, you're not going to learn.
- 01-26-2012, 08:49 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Help Please! Method that separates a string with a delimiter
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
identify the delimiter..
By saidinu in forum New To JavaReplies: 2Last Post: 09-28-2011, 05:16 PM -
How to use the split method with a non-constant delimiter
By Unnel in forum Advanced JavaReplies: 3Last Post: 01-03-2011, 03:48 PM -
Error: "The method startsWith(String, String) is undefined for the type StringUtils"
By shihad_s in forum New To JavaReplies: 10Last Post: 12-07-2010, 12:29 PM -
How do you Use Backslash as a Delimiter?
By Mobius-1 in forum New To JavaReplies: 3Last Post: 12-14-2009, 12:39 AM -
delimiter
By satin in forum New To JavaReplies: 2Last Post: 11-17-2008, 10:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks