This code is part of a method to add a word to a radix tree. if I call this method with the word in the parameter [e.g. addWord("food", node)], it throws a StringIndexOutOfBoundsException
at the char character = word.charAt(0);
line. I am struggling to figure out why that is. I have tried changing the number and still gets the same result.
private void addWord(String word, RadixNode node)
{
RadixNode childNode;
word = word.toLowerCase();
do
{
char character = word.charAt(0);
CodePudding user response:
The most likely reason is because your String word
is empty.
You should check for emptiness before:
private void addWord(String word, RadixNode node) {
if (word.isEmpty()) {
// do nothing
return;
}
...
}
You could also throw an explicit exception (eg: IllegalArgumentException
) or handle this case.
If word should not be empty, then that's mean you have to check method calling addWord
.
CodePudding user response:
Check your String word is Empty .
// CHECK YOUR STRING IS EMPTY .
if( word.isEmpty() ){
// WRITEYOURLOGIC
System.out.println("String Empty");
}
else{ // YOUR STRING IS NOT EMPTY .
word = word.toLowerCase();
do{
char character = word.charAt(0);
}while( WRITEYOURLOGIC );
}