Hi, I am new to the forum and very new to programming as well. I've got an assignment where I need to write a method to perform a linear search on an array of integers and return the position of a specific integer. The program seems to accept the user inputs just fine but always returns 0; I can't quite figure out why. Here is my code:
import java.util.Scanner;
public class ArrayOps
{
public static int findValue(int values[], int valueToFind)
{
int pos = 0;
int searchValue = valueToFind;
boolean found = false;
while (pos < values.length && !found)
{
if (values[pos] == searchValue)
{
found = true;
}
else
{
pos++;
}
}
if (found)
{
return pos;
}
else
{
return values.length;
}
}
public static void main(String[] args)
{
int valueToFind = 0;
int[] values = new int[10];
int index = findValue(values, valueToFind);
System.out.println("Enter a series of 10 integers: ");
Scanner in = new Scanner(System.in);
for (int i = 0; i < values.length; i++)
{
values[i] = in.nextInt();
}
System.out.println("Which number to find? ");
valueToFind = in.nextInt();
System.out.print(index);
}
}