I'm trying to make a phone book reader for a school project. The program asks for a first name and a last name. The names and phone numbers of people are stored in an array. If you don't enter a first name and the last name is assigned to multiple people, the program should spit out multiple peoples' phone numbers. Sorry, it might be a little long.
public static void main(String[] args) {
//Where the user input starts, gotta get dat number
Scanner scan = new Scanner(System.in);
System.out.print("First Name? ");
String first = scan.nextLine();
System.out.print("Last Name? ");
String last = scan.nextLine();
String fullname = first + last;
int[] count = new int[0];
int counter=0;
if(first.equals("quit") || last.equals("quit"))
System.out.println("Bye");
else
{
for(int l=0; l < 5; l++)
{
PhoneBook pb = new PhoneBook();
PhoneEntry entry = pb.search(fullname);
if( entry != null)
{
count[counter] = l; //this is where the issue lies
counter++;
}
}
}
if(count.length != 0)
{
for(int x=0; x<count.length; x++ )
{
PhoneBook pb = new PhoneBook();
System.out.println("Name: " + pb.phoneBook[count[x]].name + " Number: " + pb.phoneBook[count[x]].phone);
x++;
}
}
}
class PhoneEntry
{
String name; // name of a person
String phone; // their phone number
PhoneEntry( String n, String p )
{
name = n; phone = p;
}
}
class PhoneBook
{
PhoneEntry[] phoneBook;
PhoneBook() // constructor
{
phoneBook = new PhoneEntry[ 5 ] ;
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" );
}
PhoneEntry search( String targetName )
{
for (int j=0; j<phoneBook.length; j++)
{
if ( phoneBook[ j ].
//stuff is to make it so that it only tests if its to upper case and if it contains the target
name.toUpperCase().contains( targetName.toUpperCase()))
return phoneBook[ j ];
}
return null;
}
}
Any help is much appreciated.