Nikhil Kumar Nikhil Kumar - 1 year ago 58
Java Question

Find index of item in array entered by user

I have a edittext and allows user to enter the items.

`EditText editText=(EditText)findViewById(;

String s=editText.getText().toString();

String [] array= s.split("\");`

Suppose user entered
. Now I want to find the
of third item entered by user (which is E here ) .

How to find the index of third item ?

Answer Source

Here it is:

EditText editText=(EditText)findViewById(;

String str = "E";  // string you search for

String s=editText.getText().toString();

String [] array= s.split("\");
int index = -1;

for (int i = 0; i < array.length; i++) {
    if (array[i].equals(str)) { // use equals, not ==
         index = i;

So, if index is -1, there is no str in user input array. If it is bigger then -1, you get the index. (break is there so you don't need to continue trough array since you find the index).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download