user2406160
user2406160

Reputation: 538

Is it possible to search through an array and return the index of the value

I am writing a simple Java program that basically stores an array of artists that have been in the charts previously; this is my code so far for the program

package searching;

import java.util.*;

public class Searching {
    public static void main(String[] args) {          
        Scanner scanner = new Scanner(System.in);

        String artists[] = {"Rihanna", "Cheryl Cole", "Alexis Jordan", "Katy Perry", "Bruno Mars",
                        "Cee Lo Green", "Mike Posner", "Nelly", "Duck Sauce", "The Saturdays"};

        System.out.println("Please enter an artist...");
        String artist = scanner.nextLine();
    }
}

I was just wondering, is it possible for the user to type the name of one of the artists, get the code to search the array and return the index of that value? and if so how would I go about it as I don't know where to begin... thanks in advance!

Upvotes: 1

Views: 156

Answers (4)

Andrew Martin
Andrew Martin

Reputation: 5741

You could do it like so:

int index = -1;

for (int i = 0; i < artists.length; i++) {
    if (artist.equals(artists[i]))
        index = i;
}

if (index == -1)
    System.out.println("Artist not found");
else
    System.out.println("Index of artist: " + index);
}

This isn't as eloquent as tieTYT's solution, but does the trick. The index is set to -1. The for loop compares each artist to each value in your array. If a match is found, the index is set to the index of the element.

After the for loop, if the index is still -1, the user is informed that no match was found, otherwise the appropriate artist and index are output.

The user of a for loop is the most common way to scroll through the contents of an array and compare elements against a given value. By calling artists[i], every element of the array can be checked against the input String.

Upvotes: 2

Andy Thomas
Andy Thomas

Reputation: 86411

With an unsorted array, one option would be to put the artists in a List and use List.indexOf().

 List<String> artistsList = Arrays.asList( artists );
 ...
 int index = artistsList.indexOf( artist );

If the artists were sorted, you could use Arrays.binarySearch().

Upvotes: 5

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

I was just wondering, is it possible for the user to type the name of one of the artists, get the code to search the array and return the index of that value?

Yes, it is possible.

Since you don't know where to begin, I would say you can start going through the array (probably using a for loop) and validating if the artist variable is equals to the current element of the array. If they're equals, then you can just return the current index of the element of the array. If nothing is found, then return a default value like -1 that you can handle and return a message like Artist not found.

Upvotes: 3

Daniel Kaplan
Daniel Kaplan

Reputation: 67360

You need to loop through the artists array in a for loop and then return the index if the value equals the artist value.

    for (int i = 0; i < artists.length; i++) {
        String artistElement = artists[i];
        if (artistElement.equals(artist)) {
            System.out.println(i);
        }
    }

Here's what happened for me:

Please enter an artist...
Mike Posner
6

Upvotes: 4

Related Questions