Sign in to follow this  
TimothyMetcalfe

Substrings in Java

Recommended Posts

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...
[code]
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;
[/code]
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote name='tmetcalfe89' timestamp='1307330947' post='4819963']
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
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1307336987' post='4819983']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this