I have been looking for hours, all over the internet and SO, but cannot find anything that I understand! ( Very new at Java )
Upon compiling, it cannot find symbol of the contain method. Here is the code:
public class LotteryTicket {
private String nameOfBuyer;
private int[] numberList;
private boolean search(int val) {
if (val >= 1 && val <= 50) {
if (numberList.contains(val)) {
return true;
} else {
return false;
}
}
}
I am very new at learning, and I do not know why this is happening.
int[]
is a primitive array and does not have a method .contains()
. If you used List<Integer>
instead, that would give you a .contains()
method to call.
Also, your search method must return a value even when val < 1
or val > 50
.
If you need numberList
to be an int[]
, you could try this:
private boolean search(int val) {
if (numberList != null && val >= 1 && val <= 50) {
for(int number : numberList) {
if (number == val) {
return true;
}
}
}
return false;
}
Or, you could do this:
private boolean search(int val) {
if (numberList != null && val >= 1 && val <= 50) {
return Arrays.asList(numberList).contains(val);
}
return false;
}