Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing substring method in my own way.

 
Gopi Chand Maddula
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gopi Chand Maddula
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gopi Chand Maddula wrote:
some code sample would be appreciated.

er, aren't you supposed to DoYourOwnHomework?
 
Gopi Chand Maddula
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic