File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Knuth Morris Pratt Search Algorithm Help

 
Niall Watchorn
Greenhorn
Posts: 2
Chrome Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I am trying to implement a KMP Search algorithm, but I'm getting an out of bounds exception, at like 59 ( if(W[i] == S[m + i]){ )

Any ideas?

Please help

My Algorithms class:



Main method:









I found the psuedo code for the KMP Algorithm and the failure function here:
KMP Psuedo Code

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println() is your best friend.

right before your line 59, do something like

System.out.println("i is " + i ", and m is " + m + ".");

I'd also put in somewhere a "length of S is " + S.length();

then you can see what the values are just before it throws your OOB error.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that variable names should be with lowercase letters. Also I would rename the variables into something with more meaning that i, n and m.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic