У меня настроен сканер в testAddressBook, и все выглядит хорошо. Не включая весь этот файл, но основная часть, в которой я настраиваю сканер, выглядит следующим образом.
Код: Выделить всё
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();
Код: Выделить всё
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
Первый класс - Date: //Мне необходимо настроить его таким образом, хотя переменные могут быть int или строковыми
//думаю, что я бы предпочел int, но в любом случае ничего страшного
public class Date
{
частная строка dMonth;
частная строка dDay;
частная строка dYear;
//частная строка bday; попробуй..
Код: Выделить всё
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);
}
Код: Выделить всё
//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());
}
И последний класс Адресной книги, где я прочитал все эти имена с помощью устройства чтения файлов: ЭТО ГДЕ ПРОИСХОДИТ ОШИБКА, КОГДА ДОБИРАЕТСЯ ДО ДНЯ РОЖДЕНИЯ
Код: Выделить всё
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())
Подробнее здесь: [url]https://stackoverflow.com/questions/20014692/read-date-from-a-file-and-return-a-string-of-month-date-year[/url]
Мобильная версия