-3

I have two arrays (one going from 0-49 listing every US state, and another one going from 0-49 listing the US capitals.) and I need to binary search the second array to match a selected US state to capital. How do I do that?

Here's the code I've got. I should return State Capital + number of probes.

    public static int BinarySearch(String key, String[] a) {
        int lo = 0;
        int hi = a.length - 1;
        int mid = 0;
        String stateinarray = "";
        NumberofProbes = 0;
        // YOUR CODE GOES HERE
4
  • 4
    How do I ask and answer homework questions Commented Nov 27 at 5:40
  • There should be FAR more details, by you, to the portion at YOUR CODE GOES HERE, not ours! So do your part, and then when you get stuck, come back and ask about where you get stuck. Stackoverflow is not a code-writing service. Commented Nov 27 at 5:42
  • 1
    This doesn't make a lot of sense. 1) Searching for the name of a state in an array of capitals is not going to yield any results. 2) Java has a perfectly usable built-in binarySearch() method. 3) The use case you're describing should be addressed with a Map, not two arrays. 4) Tell whoever gave you that assignment to learn proper Java naming conventions. Commented Nov 27 at 5:44
  • So method BinarySearch needs to search for key in a? That method returns an int, so should the method return the index of key in a or -1 (negative one) if key is not found in a? Commented Nov 27 at 5:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.