I'm running a program that parses the text off of a website and then spell checks a string containing all of the text after it's been cleaned from HTML mark up.
Once the spell checker reaches the end of the string, it returns this this exception. I saw similar questions which were resolved by setting the index greater than 0 in an "if" statement, but I've been struggling to fix this for some time now and would appreciate some help in figuring this out.
Exception thrown:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at ParseCleanCheck.checkWord(ParseCleanCheck.java:173)
at ParseCleanCheck.SpellChecker(ParseCleanCheck.java:101)
Java line 173-175 is where the word is stripped of any punctuation marks:
if (length > 2 && word.substring(length - 2).equals(",\"") || word.substring(length - 2).equals(".\"")
|| word.substring(length - 2).equals("?\"") || word.substring(length - 2).equals("!\"")) {
unpunctWord = word.substring(0, length - 2);
Line 101 is documented below, I've added the relevant surrounding code that might be part of the exception being thrown
String user_text = "";
user_text = cleanString;
while (!user_text.equalsIgnoreCase("q")) {
// check if necessary or if cleanString still works
// PageScanner();
user_text = cleanString;
String[] words = user_text.split(" ");
int error = 0;
for (String word : words) {
suggestWord = true; // ~~~ Line 101 ~~~~
String outputWord = checkWord(word);
if (suggestWord) {
System.out.println("Suggestions for " + word + " are: " + suggest.correct(outputWord) + "\n");
error++;
}
}
if (error == 0 & !user_text.equalsIgnoreCase("q")) {
System.out.println("No mistakes found");
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
Aucun commentaire:
Enregistrer un commentaire