1

I get an error (must return an int) when trying to return the index, I can't see what I am doing wrong. How do compare the Object array indexes with an int, and return the index number?

//x starts at 10000 because the left most number is assumed to be at least a 1.
/**
 * Search for a book id within the Book object array
 * @param Book - Array of objects with book id, title, isbn, author, and category
 * @param numOfBooks - how many books are in the library
 * @param myBookID - The key to search for
 * @return the index of the array where the key matches
 */
public static int bookSearch (Object[] Book, int numOfBooks, int myBookID) {
    for (int x = 10000; x <= numOfBooks; x ++)
        if (Book[x].equals(myBookID))
            return x;
}
3
  • 1
    In Java you can get the array length by Book.length. In Java you should use lower-case for variables. Commented Feb 27, 2013 at 11:10
  • That's how my instructor typed up the instructions, with those exact variable names... Didn't want to go against it. Commented Feb 27, 2013 at 11:15
  • I suggest you change instructor/course. Commented Feb 27, 2013 at 12:05

8 Answers 8

4

You need to add an extra return at the end, because its possible your if condition never matches.

public static int bookSearch (Object[] Book, int numOfBooks, int myBookID) {
    for (int x = 10000; x <= numOfBooks; x ++) {
        if (Book[x].equals(myBookID))
            return x;
    }

    return -1;
}

On a side note, you may want to check the bounds of your array, as opposed to assuming that you have up to 10000 items in it. You can also take advantage of the fact that all arrays have a length property, to avoid passing in one of your parameters:

public static int bookSearch (Object[] books, int myBookID) {
    if(books.length < 100000) return -1;

    for (int x = 10000; x <= books.length; x++) {
        if (Book[x].equals(myBookID))
            return x;
    }

    return -1;
}
Sign up to request clarification or add additional context in comments.

7 Comments

OH DUH! Thank you so much! Of all the things I tried, I don't know why that wasn't one of them...
this will always return -1, as Book[x] is an object that will never be equal to the primitive int myBookID.
@jlordo - since he is passing in an object array I presume he is comparing Integers to ints, which is fine. The int argument will be autoboxed.
I see what you did with the second code. But my instructor wanted to pass three parameters. I appreciate the advice though.
@AndrewTram: Can you show us how the values are stored into the array?
|
2

you are probably looking for something like:

public static int bookSearch (Book[] books, int myBookID) {
    for (int x = 0; x < books.length; x++)
        if (books[x].getId() == myBookID)
            return x;
    return -1; // not found
}

Comments

2

It work only in javascript,

You can easily find index no. from object array list by any property in object

function _findIndexByKeyValue(arraytosearch, key, valuetosearch) {
    for (var i = 0; i < arraytosearch.length; i++) {
        if (arraytosearch[i][key] == valuetosearch) {
            return i;
        }
    }
    return -1;
}

Apply above method like this

var index = _findIndexByKeyValue(objectArrayList, "id", objectArrayList.id); // pass by value
if (index > -1) {
    // your code 
}

array list

var objectArrayList = [{
    property1: 10,
    property2: 11,
    timestamp: "date-001",
    id: 1
}, {
    property1: 10,
    property2: 11,
    timestamp: "date-002",
    id: 2
}, {
    property1: 10,
    property2: 14,
    timestamp: "date-002",
    id: 3
}, {
    property1: 17,
    property2: 11,
    timestamp: "date-003",
    id: 4
}];

Comments

0

There should be a return statement outside the loop so that the function returns an int even if the loop is exited without satisfying the condition.

Comments

0

You need to return a value outside the loop in case the equals never matches. Another solution is to throw exception if not found.

You cant get to an end of a function whose return type isn't void, without actually returning a value (or throwing exception).

Comments

0

This is because the compiler see farther than you, it also considers the case that your book is not in the array. What do you return then?

Comments

0

You must add a second return under the loop in case the equals never matches.

Comments

0

The problem is that the return inside the if statement might never be reached if the expression evaluated inside the if is always false.

Just add a "default" return outside the for loop!

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.