How to Match a String in an Array Using Java


The string class in Java provides a method to check whether two string values are equal. Using this method in conjunction with a loop and conditional statement, your program can find the index of the element matching a specific string in an array. To match the string, you first need to implement a loop to iterate through the array structure. Each time the loop iterates, your code can compare the current string value to the one you are looking for. The loop can continue until it finds the string or reaches the end of the structure.

  • Create a string array in your program. If you already have a string array, you can use that. Otherwise, you can create and instantiate one by using the following code:

    String[] words = {"apple", "banana", "orange", "mango", "peach"};

    The array structure now has five elements in it, with each one storing a single word. Specify the string you want to match as a variable, using the following code:

    String match = "mango";

    Alter the value of the string to reflect the text you are looking to match in your array.

  • Loop through your array. Add the following code, creating a variable to keep track of the matched array index when your program finds it:

    int matchIndex = -1;

    By starting this variable at negative one, you will know if your program found the match string or not. Add the following loop outline to search through your array:
    for(int w=0; w<words.length; w++) {
    //check the string

    This loop will iterate once for every element in your array. Inside the loop, you can compare the current string element to the one you are trying to match.

  • Compare the current array element to your match string. Add the following conditional statement inside your loop:

    if(words[w].equals(match)) {
    //specify what to do when the string is matched

    This code calls the string equals method. If the current array element string is equal to the match string, the conditional test will return a true value. Inside the conditional statement you can supply instructions for when the string is matched.

  • Specify what will happen when Java finds your string. Add the following code inside your conditional if statement:

    matchIndex = w;

    This code sets the value of the match integer variable to the index of the array element that matches the desired string. Once the match is made, no need is present for the loop to continue executing, so the break statement stops it.

  • Use the value of the matched index in your program. Your code can use the integer variable after the for loop finishes executing. The following sample code writes the value to the output console for testing:


    Experiment with the code by changing the value of the string you want to match in the array.

Tips & Warnings

  • If you want to find more than one match for your string, you can create an array to store these index positions and remove the break statement.
  • The string equals method will only return true if the two strings contain exactly the same character sequence.

Related Searches


  • Photo Credit Jupiterimages/Comstock/Getty Images
Promoted By Zergnet



Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!