valar3573
March 29th, 2008, 01:22 PM
A few of the methods arent written yet, but my problem is that when I choose to enter information, i get prompted for all the values, but when i open up the dat file, there is nothing there. The view contact method all does not print anything. I realize its a long program, but any help would be GREATLY appreciated. No one in my class has any clue, and im at my wits end.
thanks so much.
import java.io.*;
import java.util.*;
import static java.lang.System.*;
public class dossier
{
public static void main( String [] args ) throws IOException
{
Driver driver = new Driver();
driver.mainMenu();
}
}
class Contact implements Comparable
{ private String firstName;
private String lastName;
private String homePhone;
private String workPhone;
private String cellPhone;
private String email;
private String streetAddress;
private String city;
private String state;
private int zipCode;
private String country;
public int compareTo(Object o)
{
return 1;
}
public Contact()
{
firstName="";
lastName="";
homePhone="";
workPhone="";
cellPhone="";
email="";
streetAddress="";
city="";
state="";
zipCode=0;
country="";
}
public Contact(String firstName, String lastName, String homePhone,
String workPhone, String cellPhone, String email, String streetAddress,
String city, String state, int zipCode, String country)
{
this.firstName=firstName;
this.lastName=lastName;
this.homePhone=homePhone;
this.workPhone=workPhone;
this.cellPhone=cellPhone;
this.email=email;
this.streetAddress=streetAddress;
this.city=city;
this.state=state;
this.zipCode=zipCode;
this.country=country;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getHomePhone()
{
return homePhone;
}
public String getWorkPhone()
{
return workPhone;
}
public String getCellPhone()
{
return cellPhone;
}
public String getEmail()
{
return email;
}
public String getStreetAddress()
{
return streetAddress;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public int getZipCode()
{
return zipCode;
}
public String getCountry()
{
return country;
}
public int compareTo(Contact c)
{
return getLastName().compareTo(c.getLastName());
}
public String toString()
{
return "First name:\t"+firstName+"\n"+"Last name:\t"+lastName+"\n"+
"Home phone #:\t"+homePhone+"\n"+"Work phone #:\t"+workPhone+"\n"+
"Cell phone #:\t"+cellPhone+"\n"+"Email:\t\t"+email+"\n"+"Street address:\t"+
streetAddress+"\n"+"City:\t\t"+city+"\n"+"State:\t\t"+state+"\n"+ "Zip code:\t"+
zipCode+"\n"+"Country:\t"+country+"\n";
}
}
class Driver
{
private Scanner input = new Scanner(System.in);
private String firstName;
private String lastName;
private String homePhone;
private String workPhone;
private String cellPhone;
private String email;
private String streetAddress;
private String city;
private String state;
private int zipCode;
private String country;
private int menuChoice=0;
private String searchKey;
private int searchMenu;
public LinkList list = new LinkList();
public void mainMenu() throws IOException
{
readFile();
do
{
System.out.println("1: Enter contact information");
System.out.println("2: Search for a contact");
System.out.println("3: View all contacts");
System.out.println("4: Exit");
System.out.print("What would you like to do? ");
try
{
menuChoice=Integer.parseInt(input.nextLine());
System.out.println();
}
catch (NumberFormatException nfe)
{
System.out.println();
System.out.println("That is not a valid entry. Please type an integer between 1 and 4.");
System.out.println();
mainMenu();
}
if (menuChoice==1)
enterInfo();
else if (menuChoice ==2)
searchContacts();
else if (menuChoice ==3)
viewContacts();
}
while (menuChoice !=4);
}
public void enterInfo() throws IOException
{
System.out.print("First name:\t\t");
firstName= input.nextLine();
System.out.print("Last name:\t\t");
lastName= input.nextLine();
System.out.print("Home phone #:\t");
homePhone=input.nextLine();
System.out.print("Work phone #:\t");
workPhone=input.nextLine();
System.out.print("Cell phone #:\t");
cellPhone=input.nextLine();
System.out.print("Email:\t\t\t");
email=input.nextLine();
System.out.print("Street address:\t");
streetAddress=input.nextLine();
System.out.print("City:\t\t\t");
city=input.nextLine();
System.out.print("State:\t\t\t");
state=input.nextLine();
System.out.print("Zip code:\t\t");
try
{
zipCode=Integer.parseInt(input.nextLine());
System.out.print("Country:\t\t");
country=input.nextLine();
Contact person = new Contact(firstName, lastName,
homePhone, workPhone, cellPhone, email, streetAddress,
city, state, zipCode, country);
list.add(person);
System.out.println();
sortContacts();
writeFile();
System.out.println();
}
catch (NumberFormatException nfe)
{
System.out.println();
System.out.println("That is not a valid entry. Please enter a five digit zid code next time.");
System.out.println();
enterInfo();
}
}
public void sortContacts() throws IOException
{
System.out.println("Sorted.");
writeFile();
}
public void searchContacts()
{
System.out.println("For what would you like to search?");
System.out.println("1: First name");
System.out.println("2: Last name");
System.out.print("Type in your choice: ");
try
{
searchMenu=Integer.parseInt(input.nextLine());
System.out.println();
}
catch (NumberFormatException nfe)
{
System.out.println();
System.out.println("That is not a valid entry. Please type either 1 or 2.");
System.out.println();
searchContacts();
}
if(searchMenu==1)
{
System.out.print("For what first name would you like to search?");
searchKey=input.nextLine();
searchFirstName(searchKey);
}
else if(searchMenu==2)
{
System.out.print("For what last name would you like to search?");
searchKey=input.nextLine();
searchLastName(searchKey);
}
else if (searchMenu!=1 && searchMenu!=2)
{
System.out.println();
System.out.println("That is not a valid entry. Please type either 1 or 2.");
System.out.println();
searchContacts();
}
}
public void searchFirstName(String name)
{
//read in name
list.searchFirstName(name);
}
public void searchLastName(String name)
{
list.searchLastName(name);
}
public void viewContacts()
{
list.viewAll();
}
public void writeFile()throws IOException
{
list.writeToFile();
}
public void readFile()throws IOException
{
int newIndex = 0;
try
{
Scanner reader = new Scanner(new File("Contacts.dat"));
while( reader.hasNext() )
{
firstName = reader.nextLine();
lastName = reader.nextLine();
homePhone = reader.nextLine();
workPhone = reader.nextLine();
cellPhone = reader.nextLine();
email = reader.nextLine();
streetAddress = reader.nextLine();
city = reader.nextLine();
state = reader.nextLine();
zipCode = reader.nextInt();
reader.nextLine();
country = reader.nextLine();
Contact temp = new Contact (firstName, lastName,
homePhone, workPhone, cellPhone, email, streetAddress,
city, state, zipCode, country);
list.add(temp);
}
reader.close();
}
catch (FileNotFoundException e)
{
System.out.println("Sorry" + e.getMessage());
}
}
}
class LinkList
{
private ListNode front;
private ListNode back;
public LinkList()
{
front = null;
back = null;
}
public void addFirst(Comparable temp)
{
front = new ListNode(temp, front);
if(back == null)
back = front;
}
public void add(Comparable temp)
{
if(front == null)
addFirst(temp);
else
{
ListNode temp1 = back;
back = new ListNode(temp, null);
temp1.setNext(back);
}
}
public boolean remove (Contact temp)
{
ListNode current = front;
ListNode previous = front;
while(current != null)
{
previous = current;
current = current.getNext();
if (current.getValue().equals(temp))
{
previous.setNext(current.getNext());
current = null;
return true;
}
}
return false;
}
public String toString()
{
String temp = "";
ListNode p = front;
while (p!=null)
{
temp += p.getValue() + "";
p=p.getNext();
}
return temp;
}
public boolean empty()
{
return (front == null);
}
public void writeToFile()throws IOException
{
FileWriter writer = new FileWriter(new File("Contacts.dat"));
out.println("writing");
ListNode current = front;
while(current.getNext()!=null)
{
String temp2 = ((Contact)current.getValue()).getFirstName();
writer.write( temp2 + "\n");
temp2 = ((Contact)current.getValue()).getLastName();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getHomePhone();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getWorkPhone();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getCellPhone();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getEmail();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getStreetAddress();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getCity();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getState();
writer.write( temp2 + "\n" );
int temp = ((Contact)current.getValue()).getZipCode();
writer.write( temp + "\n" );
temp2 = ((Contact)current.getValue()).getCountry();
writer.write( temp2 + "\n" );
current=current.getNext();
}
System.out.println("Your contact information has been entered.");
writer.flush();
writer.close();
}
public void viewAll()
{
ListNode current = front;
while(current.getNext()!=null)
{
out.println(((Contact)current.getValue()).getFirstName());
out.println(((Contact)current.getValue()).getLastName());
out.println(((Contact)current.getValue()).getHomePhone());
out.println(((Contact)current.getValue()).getWorkPhone());
out.println(((Contact)current.getValue()).getCellPhone());
out.println(((Contact)current.getValue()).getEmail());
out.println(((Contact)current.getValue()).getStreetAddress());
out.println(((Contact)current.getValue()).getCity());
out.println(((Contact)current.getValue()).getState());
out.println(((Contact)current.getValue()).getZipCode());
out.println(((Contact)current.getValue()).getCountry());
//writer.write( temp1 + "\n" );
}
}
public Contact searchLastName(String search)
{
ListNode current = front;
while(current.getNext()!= null)
{ Contact temp = (Contact)current.getValue();
if(temp.getLastName().toLowerCase().equals(search.toLowerCase()))
{
return temp;
}
current=current.getNext();
}
return null;
}
public Contact searchFirstName(String search)
{
ListNode current = front;
while(current.getNext()!= null)
{ Contact temp = (Contact)current.getValue();
if(temp.getFirstName().toLowerCase().equals(search.toLowerCase()))
{
return temp;
}
current=current.getNext();
}
return null;
}//1528 idyllwild dr.
}
class ListNode implements Linkable
{
private Comparable value; // AP uses Object
private ListNode next;
public ListNode()
{
value = null;
next = null;
}
public ListNode(Comparable initValue, ListNode initNext)
{
value=initValue;
next=initNext;
}
public Comparable getValue()
{
return value;
}
public ListNode getNext()
{
return next;
}
public void setValue(Comparable theNewValue)
{
value = theNewValue;
}
public void setNext(Linkable theNewNext)
{
next = (ListNode)theNewNext;
}
}
interface Linkable
{
Comparable getValue();
Linkable getNext();
void setNext(Linkable next);
void setValue(Comparable value);
}
thanks so much.
import java.io.*;
import java.util.*;
import static java.lang.System.*;
public class dossier
{
public static void main( String [] args ) throws IOException
{
Driver driver = new Driver();
driver.mainMenu();
}
}
class Contact implements Comparable
{ private String firstName;
private String lastName;
private String homePhone;
private String workPhone;
private String cellPhone;
private String email;
private String streetAddress;
private String city;
private String state;
private int zipCode;
private String country;
public int compareTo(Object o)
{
return 1;
}
public Contact()
{
firstName="";
lastName="";
homePhone="";
workPhone="";
cellPhone="";
email="";
streetAddress="";
city="";
state="";
zipCode=0;
country="";
}
public Contact(String firstName, String lastName, String homePhone,
String workPhone, String cellPhone, String email, String streetAddress,
String city, String state, int zipCode, String country)
{
this.firstName=firstName;
this.lastName=lastName;
this.homePhone=homePhone;
this.workPhone=workPhone;
this.cellPhone=cellPhone;
this.email=email;
this.streetAddress=streetAddress;
this.city=city;
this.state=state;
this.zipCode=zipCode;
this.country=country;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getHomePhone()
{
return homePhone;
}
public String getWorkPhone()
{
return workPhone;
}
public String getCellPhone()
{
return cellPhone;
}
public String getEmail()
{
return email;
}
public String getStreetAddress()
{
return streetAddress;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public int getZipCode()
{
return zipCode;
}
public String getCountry()
{
return country;
}
public int compareTo(Contact c)
{
return getLastName().compareTo(c.getLastName());
}
public String toString()
{
return "First name:\t"+firstName+"\n"+"Last name:\t"+lastName+"\n"+
"Home phone #:\t"+homePhone+"\n"+"Work phone #:\t"+workPhone+"\n"+
"Cell phone #:\t"+cellPhone+"\n"+"Email:\t\t"+email+"\n"+"Street address:\t"+
streetAddress+"\n"+"City:\t\t"+city+"\n"+"State:\t\t"+state+"\n"+ "Zip code:\t"+
zipCode+"\n"+"Country:\t"+country+"\n";
}
}
class Driver
{
private Scanner input = new Scanner(System.in);
private String firstName;
private String lastName;
private String homePhone;
private String workPhone;
private String cellPhone;
private String email;
private String streetAddress;
private String city;
private String state;
private int zipCode;
private String country;
private int menuChoice=0;
private String searchKey;
private int searchMenu;
public LinkList list = new LinkList();
public void mainMenu() throws IOException
{
readFile();
do
{
System.out.println("1: Enter contact information");
System.out.println("2: Search for a contact");
System.out.println("3: View all contacts");
System.out.println("4: Exit");
System.out.print("What would you like to do? ");
try
{
menuChoice=Integer.parseInt(input.nextLine());
System.out.println();
}
catch (NumberFormatException nfe)
{
System.out.println();
System.out.println("That is not a valid entry. Please type an integer between 1 and 4.");
System.out.println();
mainMenu();
}
if (menuChoice==1)
enterInfo();
else if (menuChoice ==2)
searchContacts();
else if (menuChoice ==3)
viewContacts();
}
while (menuChoice !=4);
}
public void enterInfo() throws IOException
{
System.out.print("First name:\t\t");
firstName= input.nextLine();
System.out.print("Last name:\t\t");
lastName= input.nextLine();
System.out.print("Home phone #:\t");
homePhone=input.nextLine();
System.out.print("Work phone #:\t");
workPhone=input.nextLine();
System.out.print("Cell phone #:\t");
cellPhone=input.nextLine();
System.out.print("Email:\t\t\t");
email=input.nextLine();
System.out.print("Street address:\t");
streetAddress=input.nextLine();
System.out.print("City:\t\t\t");
city=input.nextLine();
System.out.print("State:\t\t\t");
state=input.nextLine();
System.out.print("Zip code:\t\t");
try
{
zipCode=Integer.parseInt(input.nextLine());
System.out.print("Country:\t\t");
country=input.nextLine();
Contact person = new Contact(firstName, lastName,
homePhone, workPhone, cellPhone, email, streetAddress,
city, state, zipCode, country);
list.add(person);
System.out.println();
sortContacts();
writeFile();
System.out.println();
}
catch (NumberFormatException nfe)
{
System.out.println();
System.out.println("That is not a valid entry. Please enter a five digit zid code next time.");
System.out.println();
enterInfo();
}
}
public void sortContacts() throws IOException
{
System.out.println("Sorted.");
writeFile();
}
public void searchContacts()
{
System.out.println("For what would you like to search?");
System.out.println("1: First name");
System.out.println("2: Last name");
System.out.print("Type in your choice: ");
try
{
searchMenu=Integer.parseInt(input.nextLine());
System.out.println();
}
catch (NumberFormatException nfe)
{
System.out.println();
System.out.println("That is not a valid entry. Please type either 1 or 2.");
System.out.println();
searchContacts();
}
if(searchMenu==1)
{
System.out.print("For what first name would you like to search?");
searchKey=input.nextLine();
searchFirstName(searchKey);
}
else if(searchMenu==2)
{
System.out.print("For what last name would you like to search?");
searchKey=input.nextLine();
searchLastName(searchKey);
}
else if (searchMenu!=1 && searchMenu!=2)
{
System.out.println();
System.out.println("That is not a valid entry. Please type either 1 or 2.");
System.out.println();
searchContacts();
}
}
public void searchFirstName(String name)
{
//read in name
list.searchFirstName(name);
}
public void searchLastName(String name)
{
list.searchLastName(name);
}
public void viewContacts()
{
list.viewAll();
}
public void writeFile()throws IOException
{
list.writeToFile();
}
public void readFile()throws IOException
{
int newIndex = 0;
try
{
Scanner reader = new Scanner(new File("Contacts.dat"));
while( reader.hasNext() )
{
firstName = reader.nextLine();
lastName = reader.nextLine();
homePhone = reader.nextLine();
workPhone = reader.nextLine();
cellPhone = reader.nextLine();
email = reader.nextLine();
streetAddress = reader.nextLine();
city = reader.nextLine();
state = reader.nextLine();
zipCode = reader.nextInt();
reader.nextLine();
country = reader.nextLine();
Contact temp = new Contact (firstName, lastName,
homePhone, workPhone, cellPhone, email, streetAddress,
city, state, zipCode, country);
list.add(temp);
}
reader.close();
}
catch (FileNotFoundException e)
{
System.out.println("Sorry" + e.getMessage());
}
}
}
class LinkList
{
private ListNode front;
private ListNode back;
public LinkList()
{
front = null;
back = null;
}
public void addFirst(Comparable temp)
{
front = new ListNode(temp, front);
if(back == null)
back = front;
}
public void add(Comparable temp)
{
if(front == null)
addFirst(temp);
else
{
ListNode temp1 = back;
back = new ListNode(temp, null);
temp1.setNext(back);
}
}
public boolean remove (Contact temp)
{
ListNode current = front;
ListNode previous = front;
while(current != null)
{
previous = current;
current = current.getNext();
if (current.getValue().equals(temp))
{
previous.setNext(current.getNext());
current = null;
return true;
}
}
return false;
}
public String toString()
{
String temp = "";
ListNode p = front;
while (p!=null)
{
temp += p.getValue() + "";
p=p.getNext();
}
return temp;
}
public boolean empty()
{
return (front == null);
}
public void writeToFile()throws IOException
{
FileWriter writer = new FileWriter(new File("Contacts.dat"));
out.println("writing");
ListNode current = front;
while(current.getNext()!=null)
{
String temp2 = ((Contact)current.getValue()).getFirstName();
writer.write( temp2 + "\n");
temp2 = ((Contact)current.getValue()).getLastName();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getHomePhone();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getWorkPhone();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getCellPhone();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getEmail();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getStreetAddress();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getCity();
writer.write( temp2 + "\n" );
temp2 = ((Contact)current.getValue()).getState();
writer.write( temp2 + "\n" );
int temp = ((Contact)current.getValue()).getZipCode();
writer.write( temp + "\n" );
temp2 = ((Contact)current.getValue()).getCountry();
writer.write( temp2 + "\n" );
current=current.getNext();
}
System.out.println("Your contact information has been entered.");
writer.flush();
writer.close();
}
public void viewAll()
{
ListNode current = front;
while(current.getNext()!=null)
{
out.println(((Contact)current.getValue()).getFirstName());
out.println(((Contact)current.getValue()).getLastName());
out.println(((Contact)current.getValue()).getHomePhone());
out.println(((Contact)current.getValue()).getWorkPhone());
out.println(((Contact)current.getValue()).getCellPhone());
out.println(((Contact)current.getValue()).getEmail());
out.println(((Contact)current.getValue()).getStreetAddress());
out.println(((Contact)current.getValue()).getCity());
out.println(((Contact)current.getValue()).getState());
out.println(((Contact)current.getValue()).getZipCode());
out.println(((Contact)current.getValue()).getCountry());
//writer.write( temp1 + "\n" );
}
}
public Contact searchLastName(String search)
{
ListNode current = front;
while(current.getNext()!= null)
{ Contact temp = (Contact)current.getValue();
if(temp.getLastName().toLowerCase().equals(search.toLowerCase()))
{
return temp;
}
current=current.getNext();
}
return null;
}
public Contact searchFirstName(String search)
{
ListNode current = front;
while(current.getNext()!= null)
{ Contact temp = (Contact)current.getValue();
if(temp.getFirstName().toLowerCase().equals(search.toLowerCase()))
{
return temp;
}
current=current.getNext();
}
return null;
}//1528 idyllwild dr.
}
class ListNode implements Linkable
{
private Comparable value; // AP uses Object
private ListNode next;
public ListNode()
{
value = null;
next = null;
}
public ListNode(Comparable initValue, ListNode initNext)
{
value=initValue;
next=initNext;
}
public Comparable getValue()
{
return value;
}
public ListNode getNext()
{
return next;
}
public void setValue(Comparable theNewValue)
{
value = theNewValue;
}
public void setNext(Linkable theNewNext)
{
next = (ListNode)theNewNext;
}
}
interface Linkable
{
Comparable getValue();
Linkable getNext();
void setNext(Linkable next);
void setValue(Comparable value);
}