Hi,
I have an int method that checks an array for a certain number. If that number is NOT found I won't to 'return null;'. I get this error "incompatible types"
"found : <nulltype>"
"required: int"

How can I resolve this?
thank you,
Michael

You can use Integer rather than int. null, as far as I know, can only be used with Objects. int is a primitive type whereas Integer is an Object or class type. So change the return type of your method to Integer.

if methode return type is int you must return int
i think thats you must return int.

Yes, I agree with stultuske's method of keeping the return type as int and returning -1 where the number isn't found. Besides being simple, this method is a good practice. Using null might cause the caller method to throw a Null Pointer Exception if the exceptional condition is not handled.

I thought about doing -1 but that could be 1 of the numbers.
Some possabilities:
1. Returning 1.5 as null then testing for it by ((x%1) != 0) or some isInt().
2. Someone suggested throwing an exception, how could I go about doing that?

Which seems better to ayll?

thank you all,
Michael

You could add another method to check if the number is in the array first. Then only search for it if it is there. Then you don't have to worry about returning "null", or just return 0 since it shouldn't matter (provided the programmer uses your code correctly).

I thought about doing -1 but that could be 1 of the numbers.

Yes that could certainly happen, one of the underlying assumptions there is that the array doesn't contain negative numbers. For the solution, why don't you consider returning a boolean type then ? That would suffice I guess. A true value represents the number existing in the array, a false otherwise.

> How can I resolve this?

Consider returning the zero-based position of the element in the array and -1 if the element doesn't exist.

//Sample invocation
int[] arr = {1, 2, 3, 4};
int pos = Utils.search(arr, 1);  // 0
pos = Utils.search(arr, 11);  // -1

Alternatively, take a look at the contract specified by the binarySearch method of the Arrays utility class.

returns the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Or you can make the search method return an Object representing the element in which case a `null' return is legal.

I'm going to chime in and agree with the -1 return. I had assumed you were already doing what s.o.s. mentions above about returning the index of the element. That is the way many such methods operate in the standard JDK classes. Sticking to the common idioms for things like that makes your code much easier to work with.

@s.o.s & @Ezzaral : The question uppermost in my mind here is that what makes the OP not use a boolean return type here which just specifies whether the number queried exists in the array or not. Certainly he has the number, so he doesn't need the method to return it and also what good the index number can be if he already has what is in the array at that position.

I am assuming here he doesn't intend to do some calculations that would require the index number. And if he does intend to I think he should post the method code the next time.

> I am assuming here he doesn't intend to do some calculations that
> would require the index number.

This is the very reason the API designers decided to return the zero based position rather than a simple yes/no answer; since the way the result of the search is going to be used is something the API developer can't foresee or comprehend beforehand.

Returning the zero based index is pretty useful when querying an array of objects rather than primitives since there can be two distinct objects in memory which are logically equivalent.

In short, returning a zero based index kind of *fits the bill* and seems a pretty good choice over maintaining different contracts for different situations; and that too at absolutely no overhead. Take a look at the API of the binarySearch method Arrays utility class which goes a step ahead of returning a normal zero based index.

@verruckt24: He certainly could use a boolean return if "contains()" is all he needs to know, but he doesn't really say what he needs it for. Additionally, as s.o.s. points out, returning the index of the element or -1 satisfies both the "contains()" functionality and "indexOf()" functionality at the same time, which can make for a more compact API.

hello again,
Much apologies, I failed to say that, if the number was found in the array, I want to return that number. It will only have integers. Again, I am very sorry for leaving this bit out.

Michael

if the number was found in the array, I want to return that number

Yes but what are you using to search the array? If you are using the number and want to see if it is there then you already have the number. If your method takes as argument the number to search for then you already have the number. You don't need to return it:

int number = 10;


boolean b = searchNumber(number);
if (b) {
  System.out.println("Number: "+number +" found");
} else {
  System.out.println("Number: "+number +" not found");
}

Or you might want to return the index as suggested and if not found return -1 as suggested.

If the above doesn't cover you please post what your method takes as arguments and what do you want it to do and return

hello again,
Much apologies, I failed to say that, if the number was found in the array, I want to return that number. It will only have integers. Again, I am very sorry for leaving this bit out.

Michael

and what do you want to return if the number's not there?
if you use null, you have to change to Integer instead of keeping the primitive type int, but using the position in the array if it's found and -1 if it's not found is a better approach. you'll know immediately whether or not you have that number in your array, and if so, at what location it can be found.

@s.o.s & @Ezzaral : Yes completely understand that returning the array index is a very good option in the case where you are writing a library which would be used by thousands, probably even more, and where each usage could have a different reason from the other one. This also sounds to be a good option while developing methods, that are going to be used by your programs, as there's no harm in it.
But my point here is, when the user knows that he is not building an API - he did not suggest that - and the method is going to be used in his program itself, where the purpose of the searching the number too is known beforehand, the option of returning the boolean would not be a bad one.

why not use a more straightforward option of the boolean return type.

because he wants the value returned, not a true or false

because he wants the value returned, not a true or false

But isn't he using the value as input to search if it is inside the array or not? (Read my recent post)

I am not saying that someone is correct or wrong. You are all right, but with the given information it is hard to understand what the poster really wants. That is why maybe we should wait for the poster's response to my question:

what your method takes as arguments and what do you want it to do and return

yes, he does, which may seem ridicilous the least, but he did clearly state, he wants the value returned.
whether what he wants makes much sense to us, does not always change what he wants

Yes certainly he WANTS it, but that as he said he had not mentioned before and I was extending on of our earlier discussion.

> the option of returning the boolean would not be a bad one.

But this limited implementation falls short when the functionality is extended to work for both primitives and reference types. Since this very likely looks like a homework / learning exercise, the OP needs to know the problems he can face with the boolean implementation. My point here being, the boolean approach has a distinct drawback in selected cases; the index approach works without a hitch in each and every case.

There is one point I think we can all agree on: returning the value itself if it is found is not a useful thing to do.

WOW! Lot's of interesting responses. Thank you to everyone who took the time to help. I am by nature, a C programmer. It appears Java can not easily do what I want. For the program I was working on making the function return a double, returning '0.5' for null and checking the results with (x%1 == 0) worked very well. There were many ways to handle this problem. I wanted to know if Java had a way to return null for find nothing. That was the point. Again, thank you to all of you.

Michael

There certainly are ways to return null if nothing is found, but the point of the discussion above is that there are often more useful idioms for that.

Read the thread. Both Ezzaral and S.o.S agree that returning the index (or returning -1 if it is not found) fulfills your need. And from what you've described I also agree.

Returning an Int Object sounds promising. I think I'll try it.
Thx
BTW, I did not see this idea posted before. My fault though.

This article has been dead for over six months. Start a new discussion instead.