Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - For loop not executing when trying to search ArrayList
    primarykey
    data
    text
    <p>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 <strong>I do not want it to be exact!</strong> 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.<br> 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.</p> <pre><code>//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"); } } </code></pre> <p>Variables/Objects and uses:</p> <ul> <li><strong>searchFieldListener</strong><br> The KeyListener for the JTextField called searchField for obvious reasons.</li> <li><strong>query</strong><br> The string of the text to be searched for.</li> <li><strong>i</strong><br> Why does everyone use i in loops? I guess it's a coding tradition.</li> <li><strong>nameList</strong><br> The ArrayList of names (well, duh).</li> <li><strong>searchString</strong><br> The string to be searched in (as in, try to find <em>query</em> in <em>searchString</em>).</li> <li><strong>iter</strong><br> The number of iterations the for loop has been through so far. <br/><br/><hr/> Once again I hope I have been clear enough. Thanks!</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload