Reputation: 53
Using Java and no IDE, but rather the command line, I'm supposed to write a basic-ish phone book program focusing around arrays in which the user has three options: search for a name in an array and print the name and phone number if the name is found, add a new name and phone number to the array based on user input, and delete a name and number from the array based on user input. The search function works exactly how it should, but the add function does not function properly. The delete function can be left alone for the time being since I simply haven't gotten to it yet. When I say the add function does not function properly, I basically mean that my code doesn't compile and I get errors regarding the function; I also only kind of know what the heck I'm doing.
These are the specific instructions for the 'add entry' part of the program:
"To add a new name and number to the array, first look for a cell that contains null. Then construct a new PhoneEntry object and assign its reference to that cell. If no cells contain null report an error (but don't exit the program.)"
Here's my code:
import java.util.*;
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[ 10 ] ;
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 Wood", "(913) 883-2874" );
phoneBook[5] = new PhoneEntry( null, null );
phoneBook[6] = new PhoneEntry( null, null );
phoneBook[7] = new PhoneEntry( null, null );
phoneBook[8] = new PhoneEntry( null, null );
phoneBook[9] = new PhoneEntry( null, null );
}
PhoneEntry search( String targetName )
{
for ( int i = 0 ; i < phoneBook.length ; i++ )
{
if ( phoneBook[i] != null && phoneBook[i].name.equals( targetName ) )
{
return phoneBook[i];
}
}
return null;
}
PhoneEntry addEntry( String addName, addNumber )
{
for ( int i = 0 ; i < phoneBook.length ; i++ )
{
if ( phoneBook[i].name.equals( null ) )
{
phoneBook[i] = new PhoneEntry( addName, addNumber );
}
else
{
System.out.println("Phone book is full! Delete an entry first!");
}
}
return null;
}
}
class PhoneBookComplete
{
public static void main ( String[] args )
{
PhoneBook pb = new PhoneBook();
Scanner scan = new Scanner( System.in );
String tempName, tempNumber;
// INITIAL WELCOME MESSAGE START
System.out.println();
System.out.println("**********************");
System.out.println("***** PHONE BOOK *****");
System.out.println("**********************");
System.out.println();
System.out.println(" 1 Search for an entry");
System.out.println(" 2 Add a new entry");
System.out.println(" 3 Delete an entry");
System.out.println(" 4 Quit program");
System.out.println();
System.out.print("Enter a command: ");
String userInput = scan.nextLine();
PhoneEntry entry;
System.out.println();
// INITIAL WELCOME MESSAGE END
while ( userInput != "quit" )
{
if ( userInput.equals("1") ) // NAME SEARCH
{
System.out.println(" NAME SEARCH START");
System.out.println("**********************");
System.out.println("***** NAME SEARCH ****");
System.out.println("**********************");
System.out.println();
System.out.print("Enter a name: ");
userInput = scan.nextLine();
entry = pb.search( userInput );
if ( entry != null )
{
System.out.println();
System.out.println( " " + entry.name + ": " + entry.phone );
System.out.println();
}
else if ( userInput.equals("quit") )
{
break;
}
else
{
System.out.println("Name not found.");
System.out.println();
}
System.out.println(" NAME SEARCH END");
System.out.println();
}
else if ( userInput.equals("2") ) // ADD ENTRY
{
System.out.println(" ADD ENTRY START");
System.out.println("**********************");
System.out.println("***** ADD ENTRY ******");
System.out.println("**********************");
System.out.println();
System.out.print("Enter full name: ");
tempName = scan.nextLine();
System.out.print("Enter phone number: ");
tempNumber = scan.nextLine();
entry = pb.addEntry( tempName, tempNumber );
//System.out.println("Phone book is full! Delete an entry first!");
System.out.println(" ADD ENTRY END");
System.out.println();
}
else if ( userInput.equals("3") ) // DELETE ENTRY
{
System.out.println(" DELETE ENTRY START");
System.out.println("**********************");
System.out.println("**** DELETE ENTRY ****");
System.out.println("**********************");
System.out.println();
System.out.print("Enter full name: ");
userInput = scan.nextLine();
System.out.println(" DELETE ENTRY END");
System.out.println();
}
else if ( userInput.equals("4") ) // QUIT PROGRAM
{
System.out.println();
break;
}
//System.out.println("Select a command:");
System.out.println("**********************");
System.out.println("**********************");
System.out.println("**********************");
System.out.println();
System.out.println(" 1 Search for an entry");
System.out.println(" 2 Add a new entry");
System.out.println(" 3 Delete an entry");
System.out.println(" 4 Quit program");
System.out.println();
System.out.print("Enter a command: ");
userInput = scan.nextLine();
}
System.out.println("Goodbye.");
}
}
And here are the errors that I get when trying to compile said code:
PhoneBookComplete.java:46: error: <identifier> expected
PhoneEntry addEntry( String addname, addNumber )
^
I'm completely and utterly lost at this point. When I looked up the error, I found some kinda-ish similar-ish questions on here, but they said that a constructor needed to be created, so I tried that with no positive results.
I changed the PhoneEntry addSearch method(?) in the code:
PhoneEntry addEntry( String addName, addNumber )
{
for ( int i = 0 ; i < phoneBook.length ; i++ )
{
if ( phoneBook[i].name.equals( null ) )
{
PhoneEntry[] phoneBook;
PhoneBook() // constructor
{
phoneBook = new PhoneEntry[ 10 ] ;
phoneBook[i] = new PhoneEntry( addName, addNumber );
}
}
else
{
System.out.println("Phone book is full! Delete an entry first!");
}
}
return null;
}
But I got the same error as before as well as this one:
PhoneBookComplete.java:54: error: ';' expected
PhoneBook() // constructor
^
And I know you're not supposed to actually put semicolons at the end of the first line of a method(?).
When answering, please keep in mind that I'm very new to Java, and arrays, methods and whatnot are still quite confusing to me, so if you respond with something along the lines of "You need to create a constructor in that method, man!", I would appreciate an explanation of what that means and preferably how to go about doing so. If anymore information, etc. is needed, please ask and thou shall receive. With that said, any help and explanations of anything whatsoever is appreciated and more than welcome. Thanks!
Upvotes: 1
Views: 1620
Reputation: 140
His only mistake is not having defined data type AddNumber compile id on line 46. That is, you would type:
PhoneEntry addEntry(String addName, String addNumber)
Upvotes: 0
Reputation: 394146
You should specify the types of all the arguments :
PhoneEntry addEntry( String addName, String addNumber )
This is assuming the type of addNumber is String.
I would completely re-write your addEntry
method, since it had many errors (in both syntax and logic) :
PhoneEntry addEntry( String addName, String addNumber )
{
boolean found = false;
for ( int i = 0 ; i < phoneBook.length ; i++ )
{
if ( phoneBook[i].name == null )
{
phoneBook[i] = new PhoneEntry( addName, addNumber );
found = true;
break;
}
}
if (!found) {
System.out.println("Phone book is full! Delete an entry first!");
}
return null; // I'm not sure what you wish to return here
}
You want to use the phoneBook
member of your class (the one you initialize in the constructor), not a local variable. And you should search for an empty spot in the entire array before giving a message that the phone book is full.
Upvotes: 1
Reputation: 27356
PhoneEntry addEntry( String addName, addNumber )
You need to specify the type of addNumber
.
if ( phoneBook[i].name.equals( null ) )
{
PhoneEntry[] phoneBook;
PhoneBook() // constructor
{
phoneBook = new PhoneEntry[ 10 ] ;
phoneBook[i] = new PhoneEntry( addName, addNumber );
}
}
You've declared your constructor inside another method. This is not allowed.
Upvotes: 1