3 Replies - 1921 Views - Last Post: 04 August 2010 - 01:20 AM Rate Topic: -----

#1 Guest_Matt*


Reputation:

Searching parallel arrays

Posted 03 August 2010 - 08:49 PM

I am working on a homework assignment I dont want you to do it for me just point me in the right direction.

the following is what I have so far.
import java.util.Scanner;
import java.lang.*;
public class NameDirectory {
	
static String [] names = { "Harrison, Rose"  , "James, Jean" , "Smith, William" , "Smith, Brad" };
static String [] phones = { "5552234" , "5559098" , "5551234" , "5556542" };		
			
    public static void main(String[] args) {

		String lookup; //to hold a lookup string
		

    	//create a Scanner object for keyboard input
    	Scanner keyboard = new Scanner(System.in);

    	//Get a partial name to search for

    	System.out.println ("\nEnter the first few characters of the last name to lookup: \n ");
    	
    	lookup = keyboard.nextLine(); 
	

    		for ( String person : names)
    				
    		if (person.startsWith(lookup))
    		System.out.println(person);
    			
    		
    		else 
    		System.out.println ("Does not Match");

    	
 	}
}
    



I need to be able to search the name array by entering the first three letters of the name and then
it should present the name + phone. Got the first part just dont know how to get the phone number to pop up. Any help would be appreciated I can usually do this but for some reason I cant figure it out.

Is This A Good Question/Topic? 0

Replies To: Searching parallel arrays

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Searching parallel arrays

Posted 03 August 2010 - 09:05 PM

You have to find out where in the persons array you have found a match.

usually you do it like this

	        int index=0; 

    		for ( String person : names){
    				
    		if (person.startsWith(lookup)){
    		System.out.println(person);
    	        System.out.println(phones[index]);
    		}
    		else 
    		System.out.println ("Does not Match");

    	        index++;
		
		}



But for this method to work both the arrays should have the same no of elements. Otherwise you will get a indexoutofbound exception.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: Searching parallel arrays

Posted 03 August 2010 - 09:08 PM

If you have a choice, better to make a Person class to encapsulate name and phoneNumber. Much more OO, and better design. I cover class design more in my tutorial Moving Away From Parallel Arrays if you want to check it out.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,029
  • Joined: 11-December 07

Re: Searching parallel arrays

Posted 04 August 2010 - 01:20 AM

You're using the for each syntax to finders persons name but then you're stuck when it comes to finding the phone number.

Instead if the for each syntax use the classic for(i = 0; syntax. When you find the name, i will be the index if the phone number too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1