We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I am trying to return a list of int array in a method. But i get this error, "This method must return a result type of int[]". Can someone please let me know where i am going wrong.
int[] detectAvailableFiducials() {
int[] d = new int[3];
List<FiducialFound> found = detector.detect(cam);
for ( FiducialFound f : found ) {
append(d, f.getId());
sort(d);
return d;
}
}
I am trying to get a list of Fiducials detected by the camera. Please let me know. Thanks S
Answers
If you want to return a List of int arrays, then you have to return type
List<int[]>
.It also doesn't make sense to
return d
inside thefor
loop, as that will just return the first thing infound
.No, you want lines 6 and 7 after line 8 above
d is what you want to return
The error message refers to the fact that the function not in all cases is returning d: when found is empty, the for loop is skipped and therefore nothing gets returned (which is not allowed)
Please share a working sketch (MCVE) so that it is possible to test your problem and give you feedback.
First, you are calling return inside your
for()
loop instead of after you are done looping -- that means it will only return the first FiducialFound. Is that what you meant to do?Second, your method return type in line 1 says:
int[]
. So when you callreturn d
, d must be an int[]. According to your error, d isn't an int[]. What is d? I can't tell, because you haven't shared a working sketch. If this is a class method, check your append method and see if it changes d. If it is a top-level function and you are using the Processing built-in append(), maybe this is the problem?d is an int[]
the problem is elsewhere: d is not returned in every situation - see my post
Edit: Originally, I thought above was a response from @werty37, not another solution, so my post didn't make sense. Fixed.
Yes -- as I mentioned, @werty37 has the return statement in the for loop -- see my post.
Assuming that
append()
is the built-in, then @Chrisir is correct -- the function is not returning anything whenfound
is empty, which breaks the return type.If for some weird reason you did want to return on the very first loop (?) -- which you don't -- you could also fix this by adding
return new int[3]
below the loop. Then, on an empty loop, the function/method would still return an empty set.Hi
I am writing a class called Fiducial. This class has a method which detects the number of available Fiducials after reading from camera. I am using BoofCV to detect the Fiducials. I rewrote the code as Chrisir mentioned and also used an IntList instead of an int[] return type. It seems to work now.
Here is the code:
Thanks for the help. How do i get the IntList to have only unique values. Does processing provide any such functions?
Thanks Sujith
@werty37 -- If you want unique values, you could use a HashMap instead of an IntList. That has the idea of unique keys built-in.
If you don't care about the mapping at all -- you really don't want values, just unique keys -- you could use Set. See for example Java: Good way to store unique integers.
Hi Jeremy,
I did a simple IntList hasValue() check before appending to the IntList variable. Please see code.
Please let me know.
Thanks S
Looks good!
IntList does not guarantee unique values, but is fine if you are careful about checking and not adding to it concurrently.
Besides, I'm assuming that you are going to have something like 0-10 fiducials, not thousands. Part of the reason for using it types like Set and hashes is for better lookup performance, which isn't as big an issue with very small lists.
Hi Jeremy,
Yes i am not expecting to have more than 5 fiducials. Thanks for your help.
Regards S