0
public String GetValue(String key) {
    for (int i = 1; i<=length; i++) {
        if (key.equals(keyArr[i])) {
            return valueArr[i];
        }
    }
}
public String GetKey(String value) {
    String key;
    for (int i = 1; i<=length; i++) {
        if (value.equals(valueArr[i])) {
            key = keyArr[i];
            return key;
        }
    }
}

I get an error "this method must return a result of type string". But the two arrays valueArr and keyArr are both String type arrays. I know that the value of keyArr[i] and valueArr[i] are strings because if I change the return type of the method to something else it says that it expected something else and got a string.

1
  • Does this even compile? It should be complaining about a missing return statement. Commented Jan 29, 2017 at 1:52

3 Answers 3

1

That's because it does not always return a String. E.g. if the key is not found, if (key.equals(keyArr[i])) { will never be true and hence, return valueArr[i]; will never get executed.

To fix this, you need to do the following:

  • Add return statement after for loop, to return something if no match is found, e.g.:

    public String GetValue(String key) {
        for (int i = 0; i < length; i++) {
            if (key.equals(keyArr[i])) {
                return valueArr[i];
            }
        }
        return null;
    }
    
  • Handle the return type in the calling method (i.e. add a null check or perform an appropriate action if no match found).

Sign up to request clarification or add additional context in comments.

6 Comments

you can't start indexing from 1 to <=length
@Mohsen_Fatemi corrected, thanks for pointing it out. I actually used the example from question only.
i don't know how does it work without ArrayIndexOutOfBoundException for @Johndoe ? :))
@DarshanMehta yw ;)
i meant for that to be a zero, i<=length-1 would work as well thanks
|
1

The issue less with the "of type String" and more with the "must." What happens, for example, if you pass in a string that doesn't appear in the array? In that case, your function never actually returns anything.

If you're guaranteed that the key will always exist, consider putting a line at the end like

throw new RuntimeException("Key not found?");

so that the function doesn't just drop off the end. Alternatively, try adding a line like

return null;

so that you explicitly return something in that case.

4 Comments

return null may not be the best option. Why not something like -1? It is already used in some methods like binarySearch on an array.
Why not return -1? Because return type is String. Apart from that, @templatetypedef also offered a different, perhaps preferable solution using an Exception.
Exception is better here... But returning null can become really problematic.
@GrzegorzGórkiewicz I agree that null might not be the best return value here. That was mostly showing off another way to get the error to stop, which was by always returning a value.
0

the problem is when you Write the type String you must return a String value in your method. so for Example GetValue Could be like this :

public String GetValue(String key) {
    for (int i = 0; i<keyArr.length; i++) {
        if (key.equals(keyArr[i])) {
            return valueArr[i];
        }
    }
    return "";
}

pay attention to indexes , it must start from 0 to length -1 in arrays

and your second method :

public String GetKey(String value) {
    String key = "";
    for (int i = 0; i<value.length; i++) {
        if (value.equals(valueArr[i])) {
            key = keyArr[i];
            return key;
        }
    }
    return key ;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.