java - Is it possible to search through an array and return the index of the value -
i writing simple java program stores array of artists have been in charts previously; code far 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 artist..."); string artist = scanner.nextline(); } }
i wondering, possible user type name of 1 of artists, code search array , return index of value? , if how go don't know begin... in advance!
you need loop through artists array in loop , return index if value equals artist
value.
(int = 0; < artists.length; i++) { string artistelement = artists[i]; if (artistelement.equals(artist)) { system.out.println(i); } }
here's happened me:
please enter artist... mike posner 6
Comments
Post a Comment