• Post Reply Bookmark Topic Watch Topic
  • New Topic

recursion problem in my friends final exam.  RSS feed

 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was in my friends final exam.
He had to make a recursion method that returns a number that counts how many keyword, for example "java", is appeared in a sentence.
He said he must use substring methods in the recursion method.

for example,
If a sentence is "javaisjustjava" he should return the value of 8 which is the number of character of keyword.
java = 4 and appears twice so javajava = 8.

I was trying to do it but after spending an hour, I just couldn't do it anymore.       
How should I approach this question? Please consider that I only took one semester of intro java course.

inside of main


recursion method



My code


Error Message
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14
at java.lang.String.charAt(String.java:658)
at Main.checkDouble(Main.java:7)
at Main.main(Main.java:27)
exited with non-zero status


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not how you solve problems recursively.  Loops in a recursive solution are almost always a sign that you're not doing something right. Recursion is all about breaking down a problem into smaller, similar problems. When you get to a problem that's so small that it's trivial, then you stop the recursion. Like when solving factorial(n) recursively, you stop recursing when n <= 1. Otherwise, you call factorial again with n-1.

So what's the most trivial case you can think of in determining if a keyword is a part of a given phrase? Start there. Then think of the next most trivial case.  I solved this just now in about 5 minutes by following that process. Solution is about 18 lines of code, with blank lines to space things out. That's the entire program, including the package declaration.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you another hint: If the keyword is longer than the phrase you're trying to check, is there any chance that the keyword is part of the phrase?
 
terry kang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I FOUND IT!! 


 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, terry kang.

Is the code you posted yours?  Normally, we don't post complete solutions but since it's been up for ten hours I'm going to leave it.

Posting complete solutions robs the OP of the experience of finding their own solutions.  It is also not good for us to do the homework of others.  Short snippets are okay.
 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Welcome to the Ranch, terry kang.

Is the code you posted yours?  Normally, we don't post complete solutions but since it's been up for ten hours I'm going to leave it.

Posting complete solutions robs the OP of the experience of finding their own solutions.  It is also not good for us to do the homework of others.  Short snippets are okay.


Hi Knute Snortum!
I was doing this at my friends house using his computer and I accidentally posted the solution while his account was logged in.
However, I feel so good that I figured this out. Thanks guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!