如何捕获 system.err 并向用户显示消息,而不是显示终端窗口?
不允许修改包含该行的类。继承人的代码:
入学等级:
public Entry(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
{
this.firstName = paramString1;
this.lastName = paramString2;
this.street = paramString3;
this.town = paramString4;
if (paramString5.matches("[A-Z]{2}[0-9]{1,2} [0-9]{1,2}[A-Z]{2}")) {
this.postCode = paramString5;
} else {
System.err.printf("Bad postcode: '%s'\n", new Object[] { paramString5 });
this.postCode = "???";
}
}
通讯录类:
public String add(Entry paramEntry)
{
if (paramEntry == null)
return "Error: null entry";
if (this.data.contains(paramEntry)) {
return "Error: this entry already in the book";
}
boolean bool = this.data.add(paramEntry);
if (bool) {
return " entry added";
}
return "entry could not be added";
}
扩展入门类:
public class Personal extends Entry
{
private String dob;
public Personal(String firstName, String lastName, String street, String town, String postcode, String theDOB)
{
super(firstName, lastName, street, town, postcode);
dob = theDOB;
}
当前调用从 GUI 类添加条目:
entry = Personal(firstName, lastName, street, town, postcode.toUpperCase(), dob);
message = addressbook.add(entry);
如果我目前输入错误的邮政编码,我会看到终端窗口显示“错误的邮政编码:”然后它会创建一个带有 ??? 的条目 作为邮政编码。我希望能够在不添加条目的情况下提醒用户要求另一个输入。但是,我不知道如何在不更改 Entry 类的情况下执行此操作(不允许修改 Entry 或 AddressBook 类)。