*
The moose likes Beginning Java and the fly likes Implementing substring method in my own way. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing substring method in my own way." Watch "Implementing substring method in my own way." New topic
Author

Implementing substring method in my own way.

Gopi Chand Maddula
Ranch Hand

Joined: May 11, 2010
Posts: 40

Hi Java Ranch,

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.


but after executing the code i am getting ArrayIndexOutOfBoundsException. i don't know what to do with this exception.

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: 39547
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
I see that you are trying to split the string based on spaces.... why go to all this trouble when you have:

String.split(" ") readily available ???
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

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

Sean

I love this place!
Gopi Chand Maddula
Ranch Hand

Joined: May 11, 2010
Posts: 40

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.

some code sample would be appreciated.

Thanks in Advance.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Gopi Chand Maddula wrote:
some code sample would be appreciated.

er, aren't you supposed to DoYourOwnHomework?


luck, db
There are no new questions, but there may be new answers.
Gopi Chand Maddula
Ranch Hand

Joined: May 11, 2010
Posts: 40

Darryl Burke wrote:
Gopi Chand Maddula wrote:
some code sample would be appreciated.

er, aren't you supposed to DoYourOwnHomework?


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: 2969
    
    9
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.
Gopi Chand Maddula
Ranch Hand

Joined: May 11, 2010
Posts: 40

Hi Mike,

Thanks for your Reply.

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: 2969
    
    9
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.
Gopi Chand Maddula
Ranch Hand

Joined: May 11, 2010
Posts: 40


If the problem is to divide a sting into words (separated by spaces I guess)


Yes Mike, My objective is same as you said.
that's not something you said in the original post. Are there other requirements?


My Mistake. But there are no more requirements.

Being able to define and communicate requirements is an important part of problem-solving... and of professional programming.


As i am a newbie in posting this kind of stuff i should be learning the process very soon. Thanks for information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing substring method in my own way.
 
Similar Threads
Failed to copy two dimensional array
why won't charAt work?
default value for Array elements
what is the difference between these.... String[] String... String
alphabetize a string