Beginners Programs :

Java : Check if an Array Contains a Given Number | Java Programs

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.

Output – 1:

  1. The main method begins by declaring two int variables and c. 
  2. 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.
  3. 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.
  4. 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).
  5. 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.
  6. Array is initialized having size and the user is asked to enter number of elements and as the loop runs, each input int value is stored in position of that iteration in the array a contains given number.
  7. 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.
  8. 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.
  9. 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 intpassed in as arguments.
  10. 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.
  11. Within each iteration of the loop, the conditional statement checks whether the int variable check is equal in value to the element present at position in the array.
  12. 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.
  13. 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.
  • 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:

  • 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.