Results 1 to 4 of 4
- 11-29-2010, 03:06 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
StringIndexOutOfBoundsException- Crossword Solver App.
I'm working on a crossword solver that takes user input e.g. h**s*. A text file (dictionary.txt) which contains a list of about 250,000 words is read and matching words are returned i.e. horse, house, etc.
The program kind of works sometimes but mostly i keep getting a StringIndexOutOfBoundsException. I need help. Here is the code so far:
Java Code:import java.util.ArrayList; import java.util.Collection; public class MatchingWithWildcards { public static Collection<String> getMatches(String t, String p) { Collection<String> result = new ArrayList<String>(); for (int i = 0; i < t.length(); i++) { int j = 0; int h = i; int n = p.length(); while(true){ int L = SimpleLongestCommonExtension.longestCommonExtension(p, j, t, h); if (j + 1 + L == n + 1) { result.add(t.substring(i, i + n)); break; } if (((j + L) < p.length() && p.charAt(j + L) == '*') || ((h + L) < t.length() && t.charAt(h + L) == '*')) { j = j + L + 1; h = h + L + 1; } else break; } } return result; } }
Java Code:public class SimpleLongestCommonExtension { public static int longestCommonExtension(String t1, int i1, String t2, int i2) { int res = 0; for (int i = i1; i < t1.length() && i2 < t2.length(); i++, i2++) { if (t1.charAt(i) == t2.charAt(i2)) res++; else return res; } return res; } }
Java Code:import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Collection; public class Test { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new FileReader("dictionary.txt")); String str; String input = "aa******"; while ((str = in.readLine()) != null) { if(str.length() == input.length()){ MatchingWithWildcards wild = new MatchingWithWildcards(); Collection<String> results = wild.getMatches(str, input); if(!results.isEmpty()){ System.out.println(results); } } } in.close(); } catch (IOException e) { System.out.println(e.toString()); } } }
OUTPUT:
[aardvark]
[aardwolf]
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.String.substring(String.java:1934)
at gmit.MatchingWithWildcards.getMatches(MatchingWith Wildcards.java:20)
at gmit.Test.main(Test.java:23)
- 11-29-2010, 04:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
t.substring(i, i + n)
What happens when i = t.length() - 1 (which is allowed by your loop criteria)?
ETA: Actually it's should be what happens when i = t.length() - p.length()?
- 11-29-2010, 04:23 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
So, how do I fix it? I'm not very good with loops to be honest
- 12-01-2010, 01:33 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Sudoku solver with threads
By judas in forum New To JavaReplies: 8Last Post: 05-27-2010, 03:07 PM -
How to best design a calculus solver
By xcallmejudasx in forum New To JavaReplies: 4Last Post: 06-25-2009, 05:27 AM -
StringIndexOutOfBoundsException Error
By Mayur in forum New To JavaReplies: 4Last Post: 02-01-2009, 05:10 PM -
Rubiks Cube Solver
By sufs2000 in forum Advanced JavaReplies: 0Last Post: 06-03-2008, 03:20 PM -
StringIndexOutOfBoundsException
By ravian in forum New To JavaReplies: 2Last Post: 01-29-2008, 11:25 PM


LinkBack URL
About LinkBacks

Bookmarks