开发者

For with if statement java

开发者 https://www.devze.com 2023-03-12 17:33 出处:网络
I need to chek where the char c index is on string , and if the char c is\'nt there - return -开发者_如何学运维1.

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?


Remove the else clause, it's returning -1 if the first character in the string isn't correct.

You would then also need to change the return statement at the end of the method.


Why don't you just use the built-in indexOf method? That would be a lot easier and quicker than looping through the string and testing each and every character.

But if you have to use this method for some strange reason, get rid of your else clause, because it makes the function return -1 every time the character tested is not matched.


Here is an alternative solution which also works.

public int whatIstheIndex(char c) {
    int result = -1;
    for (int i = 0; i < _st.length(); i++) {
        if (_st.charAt(i) == c) {
            result = i;
        } 
    }
    return result;
}

It's just a different way of thinking about the problem. I suppose it's slightly "worse" because it adds an extra line of code, but I hope you see how/why this works.


Your code should be like this:

public int whatIstheIndex(char c) {

        for (int i = 0; i < _st.length(); i++)

            if (_st.charAt(i) == c) {
                return i;
            } 

        return -1;

    }

Hope this helps!


Why not use String.indexOf(int) method.

public  int whatIstheIndex (char c) {
    return _st.indexOf(c);
}

Else, return a -1 only after the loop finishes:

public  int whatIstheIndex (char c) {

    for (i=0;i<_st.length();i++)

        if (_st.charAt(i) == c )  {
           return i;
        }
    }

    return -1;
}


What's happening is that it's looking at the first character, and if that doesn't match, it immediately returns -1 (and hence, doesn't continue looping through the chars until it finds the right one).

You need to return -1 only if you have finished the for loop and have not found the character. So it needs to be:

public int whatIstheIndex(char c) {
    for (i = 0; i < _st.length(); i++) {
        if (_st.charAt(i) == c) {
            return i;
        }
    }
    return -1;
}


You are always returning after looking at the first character. Your test doesn't look at other characters. A debugger would show you this. The last return i is only called if the length is 0.


Your current implementation will only ever return one of two values, 0, or -1. 0 is returned when the first index is that which the character resides, or -1 if its not found there. Remove the else clause and return -1 after you've finished the for loop to indicate that you've exhaustively searched all indexes, and found no answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消