0

I have to create a java program using either a regualr array or arraylist so I chose arralist and I named it horses but now have to create another method with in the programming inorder for a user to input/search for "trigger" and if it is found it shows the position if its not there it returns a -1. any tips would be helpful I just need help in getting it so the user as to input what they want to search for.

arraylist.add("trigger")
....
.....

  /* so I have a list of horses now I need to create another method inorder for the user to input "trigger" and return the index
  of trigger which would be 0 or if they put in "harry" it will return a -1 for some reaosn my brain is not functioning I can return
  it by having it in pre disclosed in program but cant remember how to have the user input. help me if can"



        /*public int findhorsespace(String name)
        this is what it should be put into this method inorder to return it */

Edited by ITHope

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by javaAddict
0

Just loop the array or the ArrayList until you find the element that is equals with the input. If you are using Strings use the equals method. If you find it return the index of the for loop. If the loop finishes without finding anything return -1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.