我试图读取文件,其中每一行都是一个分隔的条目;例如: 鲍勃:bob@gmail.com:bob@hotmail.com
AddressBookApp.java应该允许用户选择文件,搜索方式是按名称或电子邮件。
入门级:
public class Entry
{
public String keyName;
public List<String> emailsList;
public void Entry(String name, List<String> emails)
{
this.keyName = name;
this.emailsList = emails;
System.out.println("The name is:"+keyName);
System.out.println("Emails:" + emailsList +"\n");
}
}
AddressBook.java
public class AddressBook
{
private static Set<Entry> entryList;
private static Entry one;
public void AddressBook(Entry entry)
{
entryList = new HashSet<Entry>();
this.one = entry;
entryList.add(one);
}
public void SearchByName( String name)
{
String serchName = name;
for( Entry e: entryList)
{
// How to extract the name and list of emails of e
}
}
}
AddressBookApp.java
public class AddressBookApp
{
/** Used to obtain user input. */
private static Scanner input = new Scanner(System.in);
private static Entry singleEnt;
public static void main(String[] args)
{
String fileName, entryName;
System.out.print("Enter address book filename: ");
fileName = input.nextLine();
try
{
AddressBook addressBook = readAddressBook(fileName);
showMenu(addressBook);
}
catch(IOException e)
{
System.out.println("Could not read from " + fileName + ": " + e.getMessage());
}
}
/**
* Read the address book file, containing all the names and email addresses.
*
* @param fileName The name of the address book file.
* @return A new AddressBook object containing all the information.
* @throws IOException If the file cannot be read.
*/
private static AddressBook readAddressBook(String fileName) throws IOException
{
AddressBook addressBook = new AddressBook();
singleEnt = new Entry();
List<String> emails = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while(line != null)
{
String[] parts = line.split(":");
for(int i = 1; i < parts.length; i++)
emails.add(parts[i]);
singleEnt.Entry(parts[0], emails);
// Insert your code here to add a new address book entry.
// Note:
// parts[0] contains the person's name.
// parts[1], parts[2], etc. contain the person's email address(es).
addressBook.AddressBook(singleEnt);
line = reader.readLine();
}
reader.close();
return addressBook;
}
/**
* Show the main menu, offering the user options to (1) search entries by
* name, (2) search entries by email, or (3) quit.
*
* @param addressBook The AddressBook object to search.
*/
private static void showMenu(AddressBook addressBook)
{
boolean done = false;
while(!done)
{
int option;
System.out.println("(1) Search by name, (2) Search by email, (3) Quit");
try
{
switch(Integer.parseInt(input.nextLine()))
{
case 1:
System.out.print("Enter name: ");
String name = input.nextLine();
// Insert your code here to find an entry by name and display it.
break;
case 2:
System.out.print("Enter email address: ");
String email = input.nextLine();
// Insert your code here to find an entry by email and display it.
break;
case 3:
done = true;
break;
}
}
catch(NumberFormatException e)
{
// The user entered something non-numerical.
System.out.println("Enter a number");
}
}
}
}
答案 0 :(得分:0)
你可以在你的类中为keyName和emailList设置getter和setter方法,在创建Entry对时设置它们,
for( Entry e: entryList)
{
// Call getters here to retrieve the values of name and email
}
看看这个Entry类的例子
https://tekmarathon.com/2013/03/11/creating-our-own-hashmap-in-java/