How do I check if a input (integer) matches another value (integer) that exits within my array?
Tag : java , By : user169463
Date : March 29 2020, 07:55 AM
hope this fix your issue As you see it's not possible to just write while(value != values[]) but you can search a value in an array or check if the value is contained in a List: --- First way import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class InArrayWithAsListContains {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int value=0;
List<Integer> values = new ArrayList<Integer>();
values.add(16);
values.add(10);
values.add(11);
values.add(14);
values.add(17);
do {
System.out.println("Enter a number:");
value = input.nextInt();
} while(!values.contains(value));
}
}
int[] arr = new int[] {16,10,11,14,17};
// int[] into List<Integer> conversion
for (int i : arr) values.add(i);
import java.util.Scanner;
import java.util.Arrays;
public class Program {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int value=0;
int[] values = {16,10,11,14,17};
Arrays.sort(values);
do {
System.out.println("Enter a number:");
value = input.nextInt();
} while(Arrays.binarySearch(values, value) < 0);
}
}
|
Java8 check if a given integer is present in Integer[] array
Date : March 29 2020, 07:55 AM
will be helpful for those in need You currently have an Integer[] therefore you can utilise Stream.of instead of IntStream.of. IntStream.of only takes primitive integers whereas Stream.of is used for reference types. boolean isPresent = Stream.of(nums).anyMatch(num -> Objects.equals(num, inputNum));
boolean isPresent = Arrays.stream(nums).anyMatch(num -> Objects.equals(num, inputNum));
|
Check if array contain integer and generate integer array without duplicate elements
Tag : java , By : Chris Lomax
Date : March 29 2020, 07:55 AM
|
How do you check if a integer is in an array?
Date : March 29 2020, 07:55 AM
|
c++ check if the integer is in the array
Tag : cpp , By : Boyer C.
Date : March 29 2020, 07:55 AM
|