I have a number array.
E.g.
76543,65334,776958,8978,2436,232789
How can I check if the integers contain 7
, 8
and 9
, no matter the order?
E.g. 776958, 8978
and 232789
contain 7
, 8
and 9
, while 76543
only contains 7
I have a number array.
E.g.
76543,65334,776958,8978,2436,232789
How can I check if the integers contain 7
, 8
and 9
, no matter the order?
E.g. 776958, 8978
and 232789
contain 7
, 8
and 9
, while 76543
only contains 7
int[] ints = new int[] { 76543, 65334, 776958, 8978, 2436, 232789 };
for (int i : ints) {
boolean containsAllThree = false;
if (String.valueOf(i).contains("7") && String.valueOf(i).contains("8") && String.valueOf(i).contains("9"))
containsAllThree = true;
System.out.println(containsAllThree);
}
since you need don't need 789
but 7
, 8
and 9
to be contained you have to perform 3 checks (i merged it into the IF
condition)
You must convert this number to String and them compare is exist this subString. I think if you do this you can resolve.
boolean present = false;
int[] arr = {12123,432552,4534534};
for(int num : arr){
String aux = Integer.toString(num);
if(aux.contains("789"))
present = true;
}
Assuming you use JAVA8, you can use a stream. Map it to a String, then check if that String contains the numbers:
int[] arr = new int[] {1,2,3897};
System.out.println(Arrays.stream(arr)
.mapToObj(i -> String.valueOf(i))
.anyMatch(s -> s.contains("7") && s.contains("8") && s.contains("9")));
Depending on your exact needs, you could also use the stream to filter out the numbers that contain your numbers (or those that don't) with the .filter()
method of the Stream.
int[] arr = new int[] {1,2,3897};
List list = Arrays.stream(arr)
.filter(i -> String.valueOf(i).contains("7") && String.valueOf(i).contains("8") && String.valueOf(i).contains("9"))
.boxed()
.collect(Collectors.toList());
System.out.println(list);