1

this is my first question on stack overflow but I have some experience in Java. I am making a Java application at the moment (575 lines and counting!) and am trying to search through an ArrayList for a string. But I do not want it to be exact! Let me clarify: I want to iterate through each ArrayList element and search that string for another string. if the string is found in the ArrayList element, (for now) I want it printed to the console. I hope I have been clear enough.
Following is the relevant code. All variables are defined and the code compiles, just no output (from the search function) is printed. I am pretty sure that it is because the for loop doesn't execute, but I am puzzled as to why.

//the keylistener that calls the search() function, attached to a JTextField that the query is entered into
class searchFieldListener implements KeyListener {
    searchFieldListener() {
    }
    public void keyTyped(KeyEvent event) {
        if (event.getID() == KeyEvent.KEY_TYPED) {
            query = searchField.getText()+Character.toString(event.getKeyChar());
            System.out.println(query);
            for (i = 0; i == nameList.size(); i++) {
                search(query, i);
            }
        }
    }
    public void keyReleased(KeyEvent event) {

    }
    public void keyPressed(KeyEvent event) {

    }
}

//the troublesome search() function
void search(String query, int iter) {
    searchString = nameList.get(iter);
    System.out.println(searchString);

    if (searchString.indexOf(query) != -1) {
        System.out.println(Integer.toString(iter));
    } else {
        System.out.println("not found \n");
    }
}

Variables/Objects and uses:

  • searchFieldListener
    The KeyListener for the JTextField called searchField for obvious reasons.
  • query
    The string of the text to be searched for.
  • i
    Why does everyone use i in loops? I guess it's a coding tradition.
  • nameList
    The ArrayList of names (well, duh).
  • searchString
    The string to be searched in (as in, try to find query in searchString).
  • iter
    The number of iterations the for loop has been through so far.


    Once again I hope I have been clear enough. Thanks!

4 Answers 4

6

The reason why your for loop is not executing is because of the condition used in the loop:

for (i = 0; i == nameList.size(); i++) 
              ^^

Since the size method of the ArrayList class returns the number of elements you might want to have
i < nameList.size() instead.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! It's amazing how quickly people replied! Same to everyone else who answered, I really, REALLY appreciate it.
2
for (i = 0; i == nameList.size(); i++)

should be

for (i = 0; i < nameList.size(); i++)

Not sure if you need < or <= though, you just modify it to you needs.

Comments

2

have have a typo in your for-loop. shouldn't this:

for (i = 0; i == nameList.size(); i++) {

look like this:

for (i = 0; i < nameList.size(); i++) {

Comments

2

Several correct answers, but one aspect is missing:

for (i = 0; i < nameList.size(); i++)

This is an old-school loop. Starting from Java 1.5, you should use this idiom to iterate over an array or iterable (a List is an Iterable):

for(String s: strings){

}

This simpler syntax is a) much less error-prone and b) a common way to iterate over many different data structures, including arrays and collections.

Internally this is a shortcut for this code:

for(Iterator<String> it = strings.iterator(); it.hasNext();){
    String s = it.next();
    // your code here
}

1 Comment

ahh.. have heard about these special for loops and will research and put into practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.