我会非常感谢任何输入......我已经研究并尝试了一百万种不同的东西......提前谢谢! 我必须读取输入文件以基于给定文件创建地址簿。该文件由12个联系人组成,格式如下。(我有扫描仪,所有设置在testAddressBook中,所有看起来都不错......不包括整个文件,但我设置Scanner的基本部分如下)
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
ect....
我必须将它带入一个类ExtPerson,这是一个扩展Person(字符串名称-last和first),地址(所有字符串的街道,城市,州,邮政编码,电话),日期(可以是string或int of -month,day,year),而ExtPerson还会创建一个变量来存储此人的身份,无论是朋友,家人还是业务联系人。这需要是一个有序的列表,我必须能够排序他们的相关性,输入新的联系人,并从菜单中执行各种其他功能,包括排序生日。我无法从12/02/1999格式的文件中读取日期,并将月,日,年作为三个单独的内容返回 - 在月和日之间删除斜线。我需要将所有三个分开。我尝试了很多东西,却无法得到任何东西。这是我的程序中使用此日期的六个类中的三个的代码...我的问题是在类Addressbook中,当我在文件中读取它时,我试图在生日日期读取时不确定如何在此处阅读并存储
第一个类是Date://虽然变量可以是int或string,但我需要以这种方式设置它 //我觉得我更喜欢整个但是没什么大不了的 公共课日期 { private String dMonth; private String dDay; private String dYear; // private String 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);
}
}
下一课是ExtPerson ....
//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());
}
}
最后一个类AddressBook,我用这个文件读取器读取所有这些名称:这是生日到来的错误
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)
{
temp = bookMembers[firstOutOfOrder];
location = firstOutOfOrder;
do
{
bookMembers[location] = bookMembers[location - 1];
location--;
}
while (location > 0 && (bookMembers[location - 1].getMembername().compareTo
(temp.getMembername())<0));
bookMembers[location] = temp;
}
} //end insertionSort
public void endOfDay(PrintWriter outFile)
{
outFile.println (nMembers);
for (int index = 0; index < nMembers; index++)
{
outFile.println(bookMembers[index].getMembername());
outFile.println(bookMembers[index].getRelationship());
//ECT..FINISH LATER
}
} // end endOfDay
public int binarySearch(Person searchArg)
{
int first = 0;
int last = nMembers - 1;
int mid = 0;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (bookMembers[mid].getMembername().compareTo(searchArg) == 0)
found = true;
else if (bookMembers[mid].getMembername().compareTo(searchArg) > 0)
last = mid - 1;
else
first = mid + 1;
}
if (!found)
mid = -1; //it is an unsuccessful search
return mid;
}//end binarySearch
....需要完成所有菜单驱动选项的写作ECT ... }
答案 0 :(得分:1)
在split
对象上使用String
方法(doc),如下所示:
String bday = inFile.nextLine();
String[] parts = bday.split("/");
Date d = new Date(parts[0], parts[1], parts[2]);