I am coding a simple chatbot, and my method seems to be stuck in a loop. This below is the method, and I suspect there is a problem with the while loop, but I cannot find where I am messing up. No problems with compiling and running other than the stuck loop.
The loop below takes in a complete string, statement, loops for specific keywords, goal, and the starts looking through the string at startPos
private int findKeyword(String statement, String goal, int startPos)
{
String phrase = statement.trim();
int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);
while (psn >= 0)
{
String before = " ", after = " ";
if (psn > 0)
{
before = phrase.substring(psn-1, psn).toLowerCase();
}
if (psn + goal.length() < phrase.length())
{
after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase();
}
if (((before.compareTo("a") < 0) || (before.compareTo("z") > 0)) && ((after.compareTo("a") < 0) || after.compareTo("z") > 0))
{
return psn;
}
psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
}
return -1;
}
psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
will never rich to (psn < 0) thats the problem