Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3357486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:38:19+00:00 2026-05-18T02:38:19+00:00

this is my first question on stack overflow but I have some experience in

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:38:20+00:00Added an answer on May 18, 2026 at 2:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question on stack overflow, so be gentle. Let me first
Greetings to all! This is my first question here on stackoverflow. I have a
This is a really basic question but this is the first time I've used
I know this question might sound a little cheesy but this is the first
This question might not seem programming related at first, but let me explain. I'm
So this question will get technical – eventually – but first check out Hanselminutes
this is my first question to stackoverflow so here it goes... I use cruise
this is my first question here so I hope I can articulate it well
This is my first question so please be patient :) Background: I'm implementing an
This is my very first question so I am a bit nervous about it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.