I am trying to code the functionality of substring method of String Class
My intention is to substring the given string without using any predefined methods.
Here comes my code.
Please help me with this issue. The stack trace follows.
Does my code looks good? am i implementing it correctly?
Thanks in Advance.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35232
7
posted
0
There may be other problems, but the first if/else statement increments i, which is then used again in the second if statement. That way, i==charArray.length at the last character of the string, which causes an exception.
I think your problem is that you are calling this piece of code after you have incremented i therefore your getting the ArrayOutOfBoundsException.
You should move the 2 lines into the else statement and get rid of this if statement and make sure you put this code before you increment i
Also rather then storing the String in the String array at whatever i is you should probaly have a seperate count j for example that stores where you are currently in your String array. Or use some kind of java.util.List
Ulf Dittmer wrote:the first if/else statement increments i, which is then used again in the second if statement. That way, i==charArray.length at the last character of the string, which causes an exception.
I have noticed the problem is with the increment variable itself, and i have tried in different way using another counter in the second part but i am loosing data.
Can anyone let me know how to resolve the exception and make this work.
i am supposed to do but i want a solution which i does not get. i have tried different scenarios but nothing worked.
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2776
2
posted
0
Gopi Chand Maddula wrote:I am trying to code the functionality of substring method of String Class
My intention is to substring the given string without using any predefined methods.
I'm pretty sure that's impossible. Maybe you mean "without using any predefined methods other than toCharArray()". String concatenation using += also implicitly uses some existing methods. Or you could list some other set of methods that are explicitly allowed. But it's pretty much impossible without using any predefined methods.
Hmm, reading on, your code doesn't look very much like the substring() method. Is it possible you meant split()?
As Dawn notes, split() is already a method in the String class. But I'm guessing this is a class assignment and you have been told not to use that. You probably need to find out exactly what methods you can and can't use.
your code doesn't look very much like the substring() method. Is it possible you meant split()?
As i am thinking to divide the string into different words may be it would be split() too.
I'm guessing this is a class assignment and you have been told not to use that.
How funny. Its not a class assignment and i am not a student as well. Its an interview question in a Company which i have attended.
So tried to do this but it does not worked.
So i'm trying to do on my own as well.
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2776
2
posted
0
OK, cool. Unfortunately the question as stated remains flawed - we absolutely need some existing methods in String to achieve this. I guess at this point it's not possible to go back and ask the interviewer what they meant. So I suppose the best we can do is try to use only the most basic, simple operations from String. Which is what you're doing, I think. OK, carry on.
Gopi Chand Maddula wrote:
Mike Simmons wrote: your code doesn't look very much like the substring() method. Is it possible you meant split()?
As i am thinking to divide the string into different words may be it would be split() too.
Well, you didn't say anything about dividing the string into different words, prior to this. The rest of us are just guessing at your intent, based on the code you've shown. If the problem is to divide a sting into words (separated by spaces I guess), that's not something you said in the original post. Are there other requirements? Being able to define and communicate requirements is an important part of problem-solving... and of professional programming.