Substrings in Java

Started by
3 comments, last by TimothyMetcalfe 12 years, 10 months ago
Alright, so I have a bit of code that tries to autocomplete a field for the user as they type and the code looks a little something like this...

JTextField ingredientName;
String oldText;
ArrayList<String> autocompleteList;

...

int caretPosition = ingredientName.getCaretPosition();
String testText = ingredientName.getText().substring(0, caretPosition);
if (!oldText.equals(testText)) {
Boolean done = false;
for (int i = 0; i < autocompleteList.size() && !done; i++) {
if (testText.equals(autocompleteList.get(i).substring(0, caretPosition))) { //<---Problem
ingredientName.setText(autocompleteList.get(i));
ingredientName.setCaretPosition(caretPosition);
done = true;
}
}
}
oldText = testText;

I realize the code's probably a mess as it is, but it's a rough draft. The issue is that whenever I get to the 5th letter of a compatible entry, I get an array index out of bounds on the problem line. So say I've typed "Tort" and there's an autocomplete entry for "Tortilla", as soon as I hit "i" I get the error. This code is run on caret update for ingredientName, and yes, it's inside a functioning invokeLater so the issue's not there. The error is also happening on more than one autocomplete entry, so that's not it. It's reading the string fine, the replacement in the JTextField works beautifully. It's just bugging when it hits the 5th letter, and I can't understand why.

Heading to bed since I've been staring at this for a few hours, will take a look at any answers in the morning. Thanks in advance!
Advertisement
What is in autoCompleteList? If there's an entry in there that appears before "Tortilla" and which has only four characters, I'd expect an exception.

If you can provide a minimal but complete example for others to try it will be easier to debug.
Here's the list:
Beef
Cheddar Cheese
Lettuce
Ranch Dressing
Salsa
Tomato
Tortillas

So each of these entries is loaded into autocompleteList from an external text file.
For example, as "Tortillas" is typed, here's the text inside the JTextField along with the caret position and the eventual error.

T|omato
To|mato
Tor|tillas
Tort|illas
Torti|illas
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 5

Here's the list:
Beef
Cheddar Cheese
Lettuce
Ranch Dressing
Salsa
Tomato
Tortillas

So each of these entries is loaded into autocompleteList from an external text file.
For example, as "Tortillas" is typed, here's the text inside the JTextField along with the caret position and the eventual error.

T|omato
To|mato
Tor|tillas
Tort|illas
Torti|illas
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 5


You are making the comparison with all strings in the list, Beef is 4 characters

change:
if (testText.equals(autocompleteList.get(i).substring(0, caretPosition)))

to

if (testText.equals(autocompleteList.get(i).substring(0, Math.min(caretPosition,autocompleteList.get(i).length())))
and your problem should go away.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

You are making the comparison with all strings in the list, Beef is 4 characters

change:
if (testText.equals(autocompleteList.get(i).substring(0, caretPosition)))

to

if (testText.equals(autocompleteList.get(i).substring(0, Math.min(caretPosition,autocompleteList.get(i).length())))
and your problem should go away.


Thank you! I just figured it out on my way home and had to sigh at how simple a fix it was. Rep for the help and thanks for your time.

This topic is closed to new replies.

Advertisement