-8

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

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
shanky singh
  • 1,121
  • 2
  • 12
  • 24
  • `weather` is the wrong word in context and place you put it in. Solution for your problem convert to string and check with `contains` method – XtremeBaumer Oct 02 '17 at 08:16
  • Do you mean the digits "7", "8" and "9" present in "8978" or? – Procrastinator Oct 02 '17 at 08:18
  • @procrastinator "7","8"," 9" – shanky singh Oct 02 '17 at 08:18
  • Possible duplicate of [Java, Simplified check if int array contains int](https://stackoverflow.com/questions/12020361/java-simplified-check-if-int-array-contains-int) – Tom Oct 02 '17 at 08:49
  • @Tom that suggested answer doesn't match the requirements. it checks if the array contains e.g. `7` as single value and not if any value inside the array contains all the integer – XtremeBaumer Oct 02 '17 at 09:27
  • @XtremeBaumer That suggested question does what OP asks in his question. If he fails to explain what he ___actually___ needs in it, then it isn't my problem. – Tom Oct 02 '17 at 09:30
  • @Tom he actually explains it. It is not his problem if you can't read the question properly (which **is your** problem) – XtremeBaumer Oct 02 '17 at 09:33
  • @XtremeBaumer I guess you're right. That 4 others also thought this question unclear and that your answer is based on OPs comments doesn't matter in this case here. – Tom Oct 02 '17 at 09:43

3 Answers3

2
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)

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
0

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;
}
Raúl Garcia
  • 282
  • 3
  • 18
0

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);
JensS
  • 1,151
  • 2
  • 13
  • 20