If you would like to include where you found the item in your array (the index), you could loop through the array, make a condition that terminates once the user number is found, and prints the number and its respective index.
If you haven't learned this yet, arrays are indexed starting at zero. So, if your array is
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
then the indices are
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 import java.util.Scanner; public class Array1 { public static void main(String[] args) { int[] arrayNumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Scanner sc = new Scanner(System.in); System.out.print("Enter a value: "); int num = sc.nextInt(); boolean match = false; for (int i = 0; i < arrayNumbers.length; i++) { if (num == arrayNumbers[i]) { //you're checking if your user's 'num' is the same as a certain number in the array with the '==' operator match = true; //assign your boolean to true since it's found System.out.println("Found " + num + " at index " + i + "!"); //print their number and the index break; //no need to loop through the entire array. if you keep going with programming, you'll learn about efficiency more in depth down the road. :) } } if(!match) { //Keep this outside the for loop, otherwise it will print this statement arrayNumbers.length number of times, in this case 10 times. System.out.println("Did not find " + num + " in the array."); } } }
The output for correct and incorrect user input:
Enter a value: 7 Found 7 at index 6! //see how it gives the index? Enter a value: 45 Did not find 45 in the array. //only prints once since it wasn't in the for loop.
num.