java - Sequential search does not work as expected -


i have written program takes words user have entered, button press, , puts them in arraylist. there text field user can enter letter or word, user can search in arraylist button press. i'm using sequential search algorithm accomplish this, not work expect to; if searched word found, search function should return, , print out in textarea word found , in array found. works, first search. if word not found, function should print out word not found. works want to.

the problem after searched 1 word, , displays in arraylist can found, nothing happens when press button after that, whether entered letter/word in array or not. it's string text gets stored isn't changing. don't understand why... here below custom class of search function , main class:

public class search {     static private int i;     static string index;     static boolean found = false;  public static string sequencial (arraylist<string> list, string user) {     (int = 0; < list.size(); i++) {         if (list.get(i).equals(user)) {             index = "the word " + user  + " exist on place " + + " in arraylist";             found = true;         }     }     if (!found) {         index = "the word " + user + " not found";     }     return index; } 

my main class:

arraylist<string> list = new arraylist<string>(); arraylist<string> s = new arraylist<string>();     private void btnaddactionperformed(java.awt.event.actionevent evt) {                                               // todo add handling code here:     txtaoutput.settext("");     string word = txtfadd.gettext();     list.add(word);     (int = 0; < list.size(); i++) {         txtaoutput.append("" + list.get(i) + "\n");     } }  private void btnsearchactionperformed(java.awt.event.actionevent evt) {                                             // todo add handling code here:     string user = txtfsearch.gettext();     txtaoutput.settext("");     string index = search.sequencial(list, user);     txtaoutput.settext("" + index); } 

any appreciated!

the problem declared found variable static. when first word found, set true, , nothing ever sets false. instead of making static variable, declare local variable inside sequencial (it's spelled sequential, way) function, before for-loop.

in fact, variables you've declared static should made local. declaring static variables never idea.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -