Searching In a String Array - Problem
I'm implementing a TelephoneBook program, that will have an array of peoples names and telephone numbers hard coded into the program. The user will then search for someone who is in the array and the program will output their name and telephone number.
My program is not finished at the moment, i'm just checking it as i go through. It wont complile and there is one error. Without this error, my program should just return the position of the string they have searched for. Was wondering if anyone could help me out? Here the code...
p.s The line of code it doesn't like is highlighted
Code:
// A program that will search for a name entered by the user and output the correct telephone number
import java.util.*;
public class TelephoneBook
{
public static void main (String [] args)
{
//create Scanner
Scanner myKeyboard = new Scanner(System.in);
String [] names = {"Andy", "Bert", "Carl", "Dave", "Eric", "Fred"};
int [] numbers = {111111, 222222, 333333, 444444, 555555, 666666};
//linear search - unsorted
System.out.println(names);
System.out.println(numbers);
System.out.print("\nEnter name to be searched for: ");
String searchValue = myKeyboard.nextLine();
[COLOR="DeepSkyBlue"]String position = linearUnsorted(names, searchValue);[/COLOR]
System.out.println(searchValue + " found at location " + position);
}
public static void output(int[] arr)
{
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static int linearUnsorted(int[] arr, int item)
{
int position = 0;
while (position < arr.length && arr[position] != item)
{
position ++;
}
if (position == arr.length)
{
position = -1;
}
return position;
}
}