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.

John doe
  • 3
  • 2

3 Answers3

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.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • `return null` may not be [the best option](http://stackoverflow.com/a/1274865/4280359). Why not something like `-1`? It is already used in some methods like `binarySearch` on an array. – Grzegorz Górkiewicz Jan 29 '17 at 00:00
  • Why not return -1? Because return type is `String`. Apart from that, @templatetypedef also offered a different, perhaps preferable solution using an Exception. – Aloso Jan 29 '17 at 00:05
  • Exception is better here... But returning `null` can become really problematic. – Grzegorz Górkiewicz Jan 29 '17 at 00:10
  • @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. – templatetypedef Jan 29 '17 at 01:35
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).

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
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 ;
}
Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25