Here's a basic Java program that demonstrates how to perform a linear search on an array of integers:

public class LinearSearchExample {

public static void main(String[] args) {

int[] numbers = {3, 8, 15, 17, 23, 42}; // Array to search in

int target = 17; // Number to find

int resultIndex = linearSearch(numbers, target);

if(resultIndex == -1) {

System.out.println(target + " was not found in the array.");

} else {

System.out.println(target + " was found at index " + resultIndex + ".");



public static int linearSearch(int[] array, int toFind) {

for(int i = 0; i < array.length; i++) {

if(array[i] == toFind) {

return i; // Return the index of the found element



return -1; // Element not found



This program defines an array `numbers` that contains a set of integers. The `target` variable specifies the number to search for within the array. The `linearSearch` method iterates over each element of the array, comparing it with the target. If the target is found, the index of the target element in the array is returned. If the loop completes without finding the target, `-1` is returned to indicate that the target is not in the array.

The main method calls `linearSearch` with the array and the target number, and then prints a message indicating whether the target was found and, if so, its index in the array.