my program will ask the user his last name and his first name. after that it will do a search to see if the last name and firstname matches with the list. if it does then it prints out the phone number if not then it will say name not found which i have achieved it so far. so i was playing with the program a little bit. what i wanted to do is that when a user enters just the last name and hit enter for the first name then my program should print all the name and numbers which matches the last name. here is my code:

[B]import[/B] java.util.Scanner;
[B]class[/B] PhoneEntry
{
  String firstName;
  String lastName;    // name of a person
  String phone;   // their phone number
  PhoneEntry( String f, String l, String p )
  {
    firstName = f; lastName = l; phone = p;
  }
}
 
[B]class[/B] PhoneBook
{ 
 PhoneEntry[] phoneBook; 
 PhoneBook()    // constructor
 {
   phoneBook = [B]new[/B] PhoneEntry[ 7 ] ;
   phoneBook[0] = [B]new[/B] PhoneEntry(
    "James ", "Barclay ", "(418)665-1223");
   phoneBook[1] = [B]new[/B] PhoneEntry(
    "Grace" , "Dunbar", "(860)399-3044");
   phoneBook[2] = [B]new[/B] PhoneEntry(
    "Paul", "Kratides", "(815)439-9271");
   phoneBook[3] = [B]new[/B] PhoneEntry(
    "Violet" , "Smith", "(312)223-1937");
   phoneBook[4] = [B]new[/B] PhoneEntry(
    "John", "Smith", "(913)883-2874");
    phoneBook[5] = [B]new[/B] PhoneEntry(
    "Suleman", "Smith", "(913)883-2874");
    phoneBook[6] = [B]new[/B] PhoneEntry(
    "Violet", "Dunbar", "(913)883-2874");
    
 }
 
 PhoneEntry search( String targetName )  
 {
   [B]for[/B] ([B]int[/B] j=0; j<phoneBook.length; j++)
   {
     [B]if[/B] ( phoneBook[ j ].
       lastName.equals( targetName))
           [B]return[/B] phoneBook[ j ];
   }
   [B]return[/B] [B]null[/B];
 }
}
 
 
[B]class[/B] PhoneBookTester
{
 [B]public[/B] [B]static[/B] [B]void[/B] main (String[] args)
 {
 	String firstName="";
 	String lastName="";
   PhoneBook pb = [B]new[/B] PhoneBook();  
  Scanner scan = [B]new[/B] Scanner(System.in);
  
  [B]do[/B] {
  	
  System.out.println("Enter the last name " );
  lastName = scan.nextLine();
  [B]if[/B](lastName.equals("quit")) [B]break[/B];
  System.out.println("Enter the first name ");
  firstName = scan.nextLine();
   // search for "Violet Smith"
   PhoneEntry entry =
    pb.search( lastName ); 
 
   [B]if[/B] ( entry != [B]null[/B] )
     System.out.println( "The number is " + entry.phone );
   [B]else[/B]
     System.out.println("Name not found");
     
}[B]while[/B]([B]true[/B]);
System.out.println("Good Bye ");
 }
}

this is what i have achieved rite now:

Last Name?  SmithFirst Name? VioletThe number is: (312) 223-1937

this is what i have not achieved:

Last Name?  SmithFirst Name?Violet Smith: (312)223-1937John Smith: (812) 339-4916Suleman Smith: (913)883-2874

this is what i have achieved rite now:

Last Name?
Smith
First Name?
Violet
The number is: (312) 223-1937

this is what i have not achieved:

Last Name?
Smith
First Name?

Violet Smith: (312)223-1937John Smith: (812) 339-4916Suleman Smith: (913)883-2874

The code you posted so far will always return the first match of the last name. You never even look at the firstname that is entered, so as of yet you have not even accomplished the first part of your program.

Try the first part using John Smith rather than Viloet Smith, and with the way it is currently written you will still receive Violet Smith as the output.

ok i got it. i realized later on what you are trying to say. here is my new code.

import java.util.Scanner;
import java.util.List;
class PhoneEntry
{
  String firstName;
  String lastName;    // name of a person
  String phone;   // their phone number
  PhoneEntry( String f, String l, String p ){
    firstName = f; lastName = l; phone = p;
  }
}
class PhoneBook{
  PhoneEntry[] phoneBook;
  PhoneBook()    // constructor
  {
   phoneBook = new PhoneEntry[7 ] ;
   phoneBook[0] = new PhoneEntry(
    "James ", "Barclay ", "(418)665-1223");
   phoneBook[1] = new PhoneEntry(
    "Grace" , "Dunbar", "(860)399-3044");
   phoneBook[2] = new PhoneEntry(
    "Paul", "Kratides", "(815)439-9271");
   phoneBook[3] = new PhoneEntry(
    "Violet" , "Smith", "(312)223-1937");
   phoneBook[4] = new PhoneEntry(
    "John", "Smith", "(913)883-2874");
    phoneBook[5] = new PhoneEntry(
    "Suleman", "Smith", "(913)883-2874");
    phoneBook[6] = new PhoneEntry(
    "Violet", "Dunbar", "(913)883-2874");
  }
  PhoneEntry search( String lastName, String firstName){
    for (int j=0; j<phoneBook.length; j++){
          if ( phoneBook[ j ].lastName.equals( lastName) && phoneBook[ j ].firstName.equals( firstName))
           return phoneBook[ j ];
   }
   return null;
  }
  public PhoneEntry[] search1( String lName){
           int count=0;
    for(int j=0; j<phoneBook.length; j++) {
        if ( phoneBook[ j ].lastName.equals(lName)) count++;
    }
    if(count>0){
        int k=0;
        PhoneEntry pe[]=new PhoneEntry[count];
             for(int j=0; j<phoneBook.length; j++) {
                    if ( phoneBook[ j ].lastName.equals(lName)) 
                    pe[k++]=phoneBook[j];
             }
        return pe;
    }
    return null;
  }
/*   public void  add (int index, PhoneEntry element) {
    String[] count= new String[phoneBook.length];
    if(phoneBook.length!=null)
 
      for(int j=0; j<phoneBook.length; j++) {
        PhoneEntry pd[] = new PhoneEntry[count];
      count[j] = phoneBook[j];
   }
 
 }*/
}
class PhoneBookTester{
  public static void main (String[] args){
    String firstName="";
    String lastName="";
    PhoneBook pb = new PhoneBook();
    Scanner scan = new Scanner(System.in);
    do {
       System.out.println("Enter the last name or enter quit to terminate" );
       lastName = scan.nextLine();
       if(lastName.equals("quit")) break;
       System.out.println("Enter the first name ");
       firstName = scan.nextLine();
        // search for "Violet Smith"
      PhoneEntry entry =pb.search( lastName, firstName );
      PhoneEntry[] entry1 = pb.search1(lastName);
      if(entry1.length==0){
          System.out.println("No Match Found for :-both the entries -of -first & last names..");
      }else{
          for(int i=0;i<entry1.length;i++){
             System.out.println( entry1[i].firstName + " "+entry1[i].phone);
          }
      }
      if ( entry != null ) System.out.println( "The number is (of the entry matching both first & last name) " +entry.phone );
      else
         System.out.println("Name not found");
    }while(true);
    System.out.println("Good Bye ");
  }
}

It works fine now. no problem. now i am trying to improve more though i dont have alot of experience but i am ready to get some more experience it took me 5 hrs last night to complete this program too much debuggin and too much drinking now what i would like to do is create a menu
1) add a data
2)delete a data
3) search a data
How can i do it? i know i had to use java.util.List and a method
public void (int index, PhoneEntry element)
but for some reason it is not working. i have included this method above in my code. but had to add as a comment since i was not able to do it. Any help with a brief explanation and code will be really helpful.

If you wish to be able to add and delete entires, then you would be better served changing the PhoneBook[] to an ArrayList rather than an array. An ArrayList you can dynamically add to and subtract from and is not much different in use from an array.

If you insist on continuing to use an array, then in your add method simply create a new PhoneBook[] one element longer than the current one, then use System.arraycopy() to copy the old PhoneBook array to the new one then create a new PhoneBook item in the last slot in the same manner in which you created the original entries.

For deleting, the new array will be one shorter, then cycle through the original array copying the elements one at a time, leaving out the one that matches the entry to delete.

Neither of these suggestions, however, take into account any entry that might be listed twice.

Thankyou so much masijade. i appreciate your help. i am learning Java by myself its been like 4 months and have developed a good idea of OO programming. the author never told us to use any arraylist so i am not very familiar with it. i will definitely read Java API and will try to see if i can understand Arraylist. for your second opinion if i still want to use array my question is do i need to increase my phoneBook(array). rite now i have 7 phoneBook = new PhoneEntry[7 ] ;.
>>If you insist on continuing to use an array, then in your add method >>simply create a new PhoneBook[] one element longer than the >>current one
so i am assuming that my add function will be void ? so it dont return anything? Thanks for the great explanation. hope i will definitely avail from it. any example code not related to my program just any other program will be great if not then debugging and figuring it out is a great thing in programming.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.