Ich habe den Scanner in testAddressBook eingerichtet und alles sieht gut aus. Ohne die gesamte Datei, aber der grundlegende Teil, in dem ich den Scanner einrichte, ist wie folgt.
Code: Select all
import java.io.*;
import java.util.*;
public class testAddressBook
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("AddressBook.txt"));
//inFile.useDelimiter( "[/\n]" ); tried this but doesn't work
AddressBook myMembers = new AddressBook(inFile);
inFile.close(); //InFile.close();
Code: Select all
12 //this is an int at the beginning of the file that says how many contacts I have
Rodgers Katie //this is last and first name
12/09/1999 //this is their birthdate
1 Main St APT 6 //this is their street address
Taunton //this is their city
MA 02108 (509)245-1489 //this is state zipcode and phone numb
Family //this is their relationship to me
Malik Shelly //next person in contact file... ect...
12/08/2000
100 Lincoln Drive
Omaha
NE 68131 (402)555-1212
Friend
Die erste Klasse ist Date: //Ich muss es auf diese Weise einrichten, obwohl Variablen int oder string sein können
//Ich denke, ich würde lieber int, aber keine große Sache, so oder so
public class Date
{
private String dMonth;
private String dDay;
private String dYear;
//private String bday; Versuchen Sie es...
Code: Select all
public Date()
{
dMonth = "";
dDay = "";
dYear = "";
}
public Date(String month, String day, String year)
{
dMonth = month;
dDay = day;
dYear = year;
}
public void setDate(String month, String day, String year)//had to add void..
{
dMonth = month;
dDay = day;
dYear = year;
}
public String getMonth()
{
return dMonth;
}
public String getDate()
{
return dDay;
}
public String getYear()
{
return dYear;
}
public String getDate(String month, String day, String year)
{
return (dMonth + "-" +dDay + "-" +dYear);
}
Die nächste Klasse ist ExtPerson....
Code: Select all
//THIS CLASS EXTENDS PERSON,DATE,ADDRESS
public class ExtPerson
{
private Person name;
private Date bDay;
private Address address;
private String relationship;
public ExtPerson()
{
name= new Person ();
bDay = new Date();
address = new Address();
relationship = "";
}
public ExtPerson(String last, String first, String month, String day, String
year, String street, String city, String state, String zipcode, String phone, String
relateby)
{
name= new Person (first, last);
bDay = new Date(month, day, year);
address = new Address (street, city,state,zipcode,phone);
relationship = relateby;
}
//methd to set personal info. instance variables are set according to parameters
public void setMemberInfo(String last, String first, String month, String day, String
year, String street, String city, String state, String zipcode, String phone,String
relateby)
{
name.setName(last, first);
bDay.setDate(month, day, year);
address.setAddress(street, city,state,zipcode,phone);
relationship = relateby;
}
public void setRelationship(String relateby)
{
relationship = relateby;
}
public boolean isRelationship(String relateby)
{
return (relationship.equals(relateby));
}
public String getRelationship()
{
return relationship;
}
public Person getMembername()
{
return name;
}
public void printRelationship()
{
System.out.println("Relationship: " + relationship);
}
public void printInfo()
{
System.out.println ("Name: " + name.toString());
System.out.println ("Date of birth: " + bDay.toString ());
System.out.println ("Address: " + address.toString());
System.out.println ("Relationship: " + relationship.toString());
}
Code: Select all
import java.io.*;
import java.util.*;
public class AddressBook
{
int nMembers;//in the beginning of infile, there is an int that says how many people are
in the file
ExtPerson[] bookMembers = new ExtPerson[500];
public AddressBook(Scanner inFile )
{
String last, first, street, city, state, zipcode,phone, relateby;
String month,day,year;
//int month, day, year; not sure if I'm better off with int or string for
these..
nMembers = inFile.nextInt();
inFile.nextLine(); //discard the newline character
for (int index = 0; index < nMembers; index++)
{
bookMembers[index] = new ExtPerson();
first = inFile.next();
last = inFile.next();
//HERE IS MY PROBLEM.. BIRTHDATE IS IN FILE LIKE 12/04/1999
// THE CLASS DATE MUST INCLUDE A MONTH, DATE, YEAR
//I MUST RETURN THIS WITH THOSE THREE BUT DON'T KNOW HOW TO STRIP THEM OUT
?? bday = inFile.nextLine(); //TAKE AS STRING AND SOMEHOW IN ANOTHER CLASS
BREAK IT DOWN LATER??
//or ??
month = inFile.next();//CAN I SOMEHOW STRIP IT DOWN NOW TO STORE FOR THIS INDEX
day = inFile.next();
year = inFile.next();
street = inFile.nextLine();
city = inFile.nextLine();
state = inFile.next();
zipcode = inFile.next();
phone = inFile.next();
relateby = inFile.nextLine();
//amount = inFile.nextDouble();
inFile.nextLine(); //discard the newline character
bookMembers[index].setMemberInfo(first,last,
month,day,year,street,city,state,zipcode,phone,relateby);
}
insertionSort();
}
public void printInfo()
{
for (int index = 0; index < nMembers; ++ index)
bookMembers[index].printInfo();
}
public void insertionSort()
{
int location;
ExtPerson temp = new ExtPerson();
for (int firstOutOfOrder = 1; firstOutOfOrder < nMembers;
firstOutOfOrder++)
if (bookMembers[firstOutOfOrder].getMembername().compareTo(bookMembers
[firstOutOfOrder - 1].getMembername()) 0 && (bookMembers[location - 1].getMembername().compareTo
(temp.getMembername())
Mobile version