Oshrib
Oshrib

Reputation: 1900

For with if statement java

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?

Upvotes: 0

Views: 2504

Answers (8)

Steve B
Steve B

Reputation: 21

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.

Upvotes: 2

dogbane
dogbane

Reputation: 274888

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;
}

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38561

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.

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89209

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;
}

Upvotes: 1

Sandeep Jindal
Sandeep Jindal

Reputation: 15318

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!

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533890

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.

Upvotes: 0

James Allardice
James Allardice

Reputation: 166071

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.

Upvotes: 2

Kaj
Kaj

Reputation: 10959

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.

Upvotes: 3

Related Questions