-2
public static int getIndex(String value, Constants type) {
    int myPosition = -1;
    for (int i = 0; i < type.length; i++) {
        if (type[i] == value) {
            myPosition = i;
            break;
        }
    }
    return myPosition;
}

I was thinking of a generic method where i pass the value and Constants into the method to get the required index of the string value. Its pretty much easy to do in JS, but i am not sure how can i toss a method of same in java.

CommonMethods.Utils.getIndex("Kevin", Contants.Name);

The above method is not working, as it says "length cannot be resolved or is not a field"

Kevin
  • 23,174
  • 26
  • 81
  • 111
  • 3
    What is `Constants`? An enum? An interface? A class? The attribute `length` is valid on arrays (and when `length` is explicitly declared on a class or interface). – rgettman May 08 '13 at 22:52
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo May 08 '13 at 22:53
  • 1
    What *should* the code do? – wchargin May 08 '13 at 22:54

2 Answers2

3

Your Constants object should be some sort of Collection. Look those up. There's a lot of them with different attributes.

What you seem to be rewriting here is a ArrayList. Those already have a function to search inside called indexOf.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • How do i get the index of my object which is actually mapped to my Array... lets take my array contains three objects ["sas","asas","asas"]. ArrayList.indexOf(myObject) would return me the index of the Array items or index of the List. – Kevin May 08 '13 at 23:14
1

type is a Constants, not an array. You can't run type.length because Constants does not define a length property.

If Constants is an enum you can use Constants.values() to get an array of all constants.

wchargin
  • 15,589
  • 12
  • 71
  • 110