hello,

when compiling this method, i get a "missing return statement error" because there is no return statement after the while loop and before the method closes. i understand that there needs to be one there, i'm just not sure what the value should be. i have commented out the area where i am having trouble with "//". thanks in advance.

/**
     * lookup() should return values.get(i) if we find i for which 
     * keys.get(i).equals(s), or null if not found
     */   
   
 public String lookup (String s) {
        int i = 0;
        while (i<keys.size()) {
            if (keys.get(i).equals(s)) {
                return values.get(i);
            }
            else {
                return null;
        }
      }
       //return ? ;
    }

you could try to return null, but that code is incorrect.

thank you for the reply. unfortunately, i don't see where the code is incorrect. there is more to it than what i posted. i need to start with the String lookup method, in order work to on the rest. for context, here is the entire, yet incomplete, code:

import java.util.ArrayList;
/**
 * Class StringMap manages a list of (key, value) pairs, 
 * where each of key and value is a String.
 * Methods are provided to look up the value corresponding to a key,
 * update the value corresponding to a key, and insert new pairs.
 * 
 */
public class StringMap
{
    // instance variables - replace the example below with your own
    private ArrayList<String> keys;
    private ArrayList<String> values;

    /**
     * Constructor for objects of class StringMap
     */
    public StringMap()
    {
        keys = new ArrayList<String>();
        values = new ArrayList<String>();
    }

    /**
     * lookup() should return values.get(i) if we find i for which 
     * keys.get(i).equals(s), or null if not found
     */   
    [b]public String lookup (String s) {
        int i = 0;
        while (i<keys.size()) {
            if (keys.get(i).equals(s)) {
                return values.get(i);
            }
            else {
                return null;
        }
      }
      //return ?;
    }
   [/b]
    /**
     * insert() should return false if the given key is already there,
     * and otherwise add both key and value to their respective lists,
     * at the same position.
     */
    public boolean insert(String key, String value) {
        return false;
    }
    
    /**
     * for update(), first look if key is present in keys.
     * If yes, use values.set() to update the corresponding value;
     * if no, insert <key,value> as a new pair.
     */
    public void update (String key, String value) {
    }
    
    /**
     * printmap() just prints each <key,value> pair neatly,
     * one per line.
     */
    public void printMap() {
    }
    
    public String search(String sub) {
        return null;
    }
    
    /** 
     * The following is optional; it is to look up the position
     * of String s in the ArrayList keys and return the i for which
     * keys.get(i).equals(s). If s is not found, return -1.
     * findIndex() can be *used* to write lookup() and update().
     */
    private int findIndex(String s) {
        return -1;
    }
}

i know there is more to the code, i am just saying it is incorrect. well the code is incorrect based on the commenting you have on the method. if you read the code, it doesn't actually do what the commenting says it should.

i see. that's what i was afraid of, because i still don't see where it is wrong. any hints? perhaps what the code i wrote would do in the correct context?

my hint would be "just because you have an 'if' doesn't mean you need an 'else'". make sure you understand the consequences of using a "return" statement too. if you work this out, it will also answer the original question you had. let me know how you go...

so, just taking out the else statement and adding "return null;" in the appropriate place is not the entire solution. and i obviously don't quite understand how return statements work. with the following:

public String lookup (String s) {
        int i = 0;
        while (i<keys.size()) {
            if (keys.get(i).equals(s)) {
                return values.get(i);
            }
      }
     return null;
}

it seems that null would be returned regardless of the while loop or any return statement earlier in the code.

also, are both the while loop and if statement not setup correctly? am i missing a boolean statement somewhere?

that isn't working? that code looks correct to me. as soon as your method sees a return statement it will terminate (it has found what it wanted and 'returns' the value to the caller). in your previous code you have an if / else and so each execution of the while loop generated a return (so the loop never actually looped).

if lookup() is always returning null, it means that .equals is never evaluating to true, OR the values.get(i) is generating null. in this instance though, i think the method is correct, and the fact that you haven't implemented your other function (e.g. insert) is the reason you are getting null.

do you actually initialise keys / values?

oh, excuse my ignorance. without insert() completed, of course "null" will be returned.

i really appreciate your help.

no worries, let us know if you run into any more issues.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.