views:

127

answers:

3

Hi all.

Problem:

I am using Java Tutorials™ sourcecode for this. This is the source code.

I tried this:

--following with another section of sorted words--
words.add("count");
words.add("cvs");
words.add("dce"); 
words.add("depth");
--following with another section of sorted words--

and it works perfectly. However when I use this:

--just a section of sorted words--
words.add("count");
words.add("cvs");
words.add("dce_iface"); 
words.add("dce_opnum");
words.add("dce_stub_data");
words.add("depth");
--following with another section of sorted words--

It does show dce_iface when I type dce, but when I type _ then following with o or s it shows me something else like dce_offset where the offset comes from words.add("fragoffset"); somewhere in the list.

What can I do to solve this problem? Thank you in advance.

+1  A: 

It's probably because of these lines in the code:

for (w = pos; w >= 0; w--) {
    if (! Character.isLetter(content.charAt(w))) {
        break;
    }
}

_ is not a letter character, so it treats it the same way as a space. You can try changing the condition to:

char c = content.charAt(w);
if (! (Character.isLetter(c) || c == '_')) {
robinst
OT -- Hmm weird suddenly my Chrome didn't show the textarea for the add comment. Made me puzzled for a while.@robinst -- Ah I see. Thanks!
Alex Cheng
+1  A: 

I guess you have to add the underscore as "letter" here

        // Find where the word starts
        int w;
        for (w = pos; w >= 0; w--) {
            if (!Character.isLetter(content.charAt(w))) {
                break;
            }
        }
PeterMmm
+1  A: 

It has to do with this section in insertUpdate():

// Find where the word starts
int w;
for (w = pos; w >= 0; w--) {
    if (! Character.isLetter(content.charAt(w))) {
        break;
    }
}

Specifically, Character.isLetter() returns false for the underscore character. That means that the word starts after the underscore position.

To solve it, you need to modify the if statement to allow any non letter characters you want to use in the words. You could explicitly check for '_' or use Chacter.isWhiteSpace() to include all characters that aren't a space, tab or newline.

Colin Gislason
@Colin: +1 for detailed explanation.
Alex Cheng