Liest das Datum aus einer Datei und gibt eine Zeichenfolge mit Monat, Datum und Jahr zurückJava

Java-Forum
Anonymous
 Liest das Datum aus einer Datei und gibt eine Zeichenfolge mit Monat, Datum und Jahr zurück

Post by Anonymous »

Ich muss eine Eingabedatei lesen, um ein Adressbuch basierend auf der angegebenen Datei zu erstellen. Die Datei besteht aus 12 Kontakten und hat das folgende Format.
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();
Das Format der Eingabedatei sieht so aus:

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
Ich muss dies in eine Klasse ExtPerson bringen, die eine Klasse ist, die Person(String Name -last und first), Address(alle Strings von Straße, Stadt, Bundesland, Postleitzahl, Telefon), Date (kann ein String oder ein Integer von -Monat, Tag, Jahr sein) erweitert und ExtPerson auch eine Variable erstellt, um zu speichern, wie mir diese Person bekannt ist, ob ein Freund, eine Familie oder ein Geschäftskontakt. Dies muss eine geordnete Liste sein und ich muss in der Lage sein, sie nach ihrer Beziehung zu sortieren, neue Kontakte einzugeben und verschiedene andere Funktionen über ein Menü auszuführen, einschließlich der Sortierung nach Geburtstag. Ich habe Probleme damit, das Datum aus der Datei im Format 02.12.1999 zu lesen und Monat, Tag und Jahr als drei separate Dinge zurückzugeben – indem ich die Schrägstriche zwischen dem Monat, dem Tag und dem Jahr entferne. Ich muss alle drei getrennt haben. Ich habe zahlreiche Dinge ausprobiert, aber es gelingt mir nicht, irgendetwas zum Laufen zu bringen. Hier ist mein Code für drei der sechs Klassen in meinem Programm, die dieses Datum verwenden ... Mein Problem liegt in der Klasse AddressBook, wenn ich die Datei einlese. Es geht kaputt, wenn ich versuche, das Geburtstagsdatum einzulesen. Ich bin mir nicht sicher, wie ich das einlesen und speichern soll.
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());
}
Und letzte Klasse AddressBook, in die ich alle diese Namen mit einem Dateireader eingelesen habe: HIER KOMMT DER FEHLER, WENN DER GEBURTSTAG KOMMT

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())

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post