Results 1 to 5 of 5
  1. #1
    DillMan is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Question 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

    Java 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;	
        	}
    }
    Last edited by DillMan; 12-07-2008 at 07:10 PM.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default easy...

    You don't mention the error msg you're getting but it probably has to do with the following:
    • you're trying to send a argument that's a String when the linearUnsorted method is expecting an int.


    Java Code:
    [COLOR="Red"][B]String searchValue[/B][/COLOR] = myKeyboard.nextLine();
    .
    .
    .
    String position = linearUnsorted(names, [B][COLOR="red"]searchValue[/COLOR][/B]);
    .
    .
    .
    public static int linearUnsorted(int[] arr, [B][COLOR="red"]int item[/COLOR][/B])
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    DillMan is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    public static int linearUnsorted(String[] arr, String item)

    With the method above now changed to expect a String, i still get the same error. Which is on the line below:

    String position = linearUnsorted(names, searchValue);
    .........................................^

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    it looks as if the method is returning an int and you're expecting it to be a String.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Rethink the program...

    Dillman... you have stated that you haven't finished your program so I'll just suggest some things:
    • In your program you are trying to print the two arrays (names & numbers) using the println method:

    Java Code:
    //linear search - unsorted
    	System.out.println(names);
    	System.out.println(numbers);
    This will not print what you want. I suggest you use the output method that's listed. NOTE: the current output method will only print int arrays (won't work with the names array).
    • You have to rethink/redo the linearUnsorted method. It's been coded to work with an int array and an int position. It can't receive a string as an argument. I'm not really sure what it should do. Again, you're going to have to rethink this method.


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 09:50 AM
  2. Searching a String from Text file using Swings .
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 4
    Last Post: 09-09-2008, 05:29 AM
  3. Problem with searching while using GUI
    By BHCluster in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 06:56 PM
  4. Replies: 0
    Last Post: 04-14-2008, 08:39 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •