I need to chek where the char c index is on string , and if the char c is’nt there – return -1.
public class Find {
private String _st;
int i;
public Find(String st) {
_st = st;
}
public int whatIstheIndex(char c) {
for (i=0;i<_st.length();i++)
if (_st.charAt(i) == c) {
return i;
} else {
return -1;
}
return i;
}
}
I’m getting always -1. Why? Is the last return i; unnecessary?
Your code should be like this:
Hope this helps!