Here is we have to check if an array contains a value Java Program. Along with that, we will provide you with suitable examples and sample output.
This code is for checking whether a user-determined element is present in an array using Java language.
- The problem here is to check whether a given element is present in an array.
- Our constraint here is the ability to only check for the presence of int values.
- The input here is a int value taken from the user.
- The output is a boolean value of true or false representing whether the input int value is present in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.util.Scanner; class CheckNumber { public static void main(String arg[]) { int n,c; Scanner sc=new Scanner(System.in); System.out.println("Enter number of elements "); n=sc.nextInt(); int a[]=new int[n]; System.out.println("Enter " +n+" elements"); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println("Enter the number to check "); c=sc.nextInt(); System.out.print("Check the number "+n+" from the array("); for(int i=0;i<n;i++) { System.out.print(a[i]+","); } System.out.println(")----->"+check(a,c)); } static boolean check(int a[],int check) { for(int i=0;i<a.length;i++) { if(a[i]==check) return true; } return false; } } |
Output – 1:
- The main method begins by declaring two int variables n and c.
- A new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java.
- Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
- The next line prints a statement to the console instructing the user to enter the number of elements (i.e. the size of the array to be defined).
- The .nextInt() method is called by the Scanner object sc. This method reads an int value input by a user and stores it to the int variable n.
- Array a is initialized having size n and the user is asked to enter n number of elements and as the loop runs, each input int value is stored in position i of that iteration in the array a contains given number.
- The next line prints a statement to the console instructing the user to enter a number to check whether it is present in the array.
- The .nextInt() method is called by the Scanner object sc. This method reads an int value input by a user and stores it to the int variable c.
- In the next few lines, each element in the array i.e. all the user-defined elements is printed out to the console and within the print statement in the final line, the method check is called with array a and int c passed in as arguments.
123456static boolean check(int a[],int check) {for(int i=0;i<a.length;i++) {if(a[i]==check) return true;}return false;} - In this method, a for loop iterates over each element of the array unless the element being checked is found in the array in which case, the implementation of the method terminates.
- Within each iteration of the loop, the conditional statement checks whether the int variable check is equal in value to the element present at i position in the array.
- If this condition is satisfied for any iteration of the loop, the method returns a boolean value of true. In the case that the conditional statement is not satisfied for any of the iteration, a boolean value of false is returned.
- This returned boolean value is printed out by the print statement in the last line of the main method to see if the array contains a value Java.
1 2 3 4 5 6 7 8 9 10 11 12 |
>java CheckNumber Enter number of elements 5 Enter 5 elements 1 2 3 4 5 Enter the number to check 3 Check the number 5 from the array(1,2,3,4,5,)----->true |
- Here, the element to check is the number 3. The number 3 is present at index position 2 in the array.
- The conditional statement in the method check gets satisfied in the third iteration of the loop and hence a boolean value of true is printed out.
Output – 2:
1 2 3 4 5 6 7 8 9 10 11 12 |
>java CheckNumber Enter number of elements 5 Enter 5 elements 1 1 2 1 1 Enter the number to check 3 Check the number 5 from the array(1,1,2,1,1,)----->false |
- Here, the element to check is the number 3. The number 3 is not present at any index position in the array contains a given number.
- The conditional statement in the method check does not get satisfied in any iteration of the loop and hence a boolean value of false is printed out.