简体   繁体   English

Java从listArray中删除条目

[英]Java remove entry from listArray

I have an assignment to create an address book. 我有一个任务来创建一个地址簿。 I created my program based on good usability. 我基于良好的可用性创建了程序。 For instance when creating a new entry a user cannot move onto the next input until a valid one has been given or they choose to cancel. 例如,当创建一个新条目时,用户不能移动到下一个输入,直到给出了有效的输入或他们选择取消为止。 After I created the program I was reading through the colleges examples the tutor had given and instead of checking for valid inputs before the entry is added they send the .add request and then raise an exception if bad data is detected. 创建程序后,我正在阅读导师给的大学示例,而不是在添加条目之前检查有效输入,而是发送.add请求,然后在检测到错误数据时引发异常。

I asked my tutor if I was supposed to do it this way as even though I think mine is better design I dont want to lose marks because I didnt do it their way. 我问我的导师是否应该这样做,即使我认为我的设计更好,我也不想因为没有按照自己的方式做而失去分数。 He said I should stick to the examples as follows: 他说我应该坚持以下例子:

public AddressBook()
{
    entries = new ArrayList < Entry >();
}

public void addPerson(Entry addPerson)
{
    entries.add(addPerson);
}

private void addCommand()
   {      

    System.out.print("Enter Full Name : ");
    String name = myScanner.nextLine();
    System.out.print("Enter House No and Street name: ");
    String street = myScanner.nextLine();
    System.out.print("Enter Town: ");
    String town = myScanner.nextLine();
    System.out.print("Enter Postcode: ");
    String postcode = myScanner.nextLine();
    postcode = postcode.toUpperCase();

    addressBook.addPerson(new Entry(name, street, town, postcode));        
   }

public Entry(String strName, String strStreet, String strTown, String strPostcode)
    {
       name = strName;
       street = strStreet;
       town = strTown;
       postcode = strPostcode;
       try
       {
          checkDetails();
       }
       catch ( BadDataException e)
        {
            throw new RuntimeException( e.getMessage());             }
    }

I tried it this way and changed the: 我以这种方式尝试过并更改了:

throw new RuntimeException(e.getMessage()); 

line to read 读行

System.out.println( e.getMessage());

So that it wouldnt quit out of the program however doing it this way already adds the entry so after giving the appropriate error I need to remove that entry that has been added. 这样它就不会退出程序,但是以这种方式进行操作已经添加了该条目,因此在给出适当的错误后,我需要删除已添加的条目。 How can I do this? 我怎样才能做到这一点? does it have some sort of index? 它有某种索引吗? I dont know why the tutor would want you to do it this way or am I missing the point? 我不知道为什么导师会希望您这样做,还是我错过了要点?

If an exception is thrown in the Entry constructor which is called here: 如果在Entry构造函数中抛出异常,则在此处调用该异常:

addressBook.addPerson(new Entry(name, street, town, postcode));

It won't be added to your list. 它不会被添加到您的列表中。 Just leave the code as it was and catch the exception here: 只需保留原来的代码并在此处捕获异常即可:

try{
   addressBook.addPerson(new Entry(name, street, town, postcode));
catch(Exception e){
   //Tell the user what he did wrong and let him reenter
}

You can declare a checked exception in your constructor, ie an exception that extends Exception rather than RuntimeException . 您可以在构造函数中声明一个检查的异常,即,一个扩展了Exception而不是RuntimeException Exception You will then be forced to handle it in your addCommand method. 然后,您将被迫在addCommand方法中对其进行处理。

public AddressBook() {
    entries = new ArrayList< Entry>();
}

public void addPerson(Entry addPerson) {
    entries.add(addPerson);
}

private void addCommand() {

    System.out.print("Enter Full Name : ");
    String name = myScanner.nextLine();
    System.out.print("Enter House No and Street name: ");
    String street = myScanner.nextLine();
    System.out.print("Enter Town: ");
    String town = myScanner.nextLine();
    System.out.print("Enter Postcode: ");
    String postcode = myScanner.nextLine();
    postcode = postcode.toUpperCase();
    final Entry entry;
    try {
        entry = new Entry(name, street, town, postcode);
    } catch  (BadDataException ex) {
        System.out.println(ex.getMessage());
        return;
    }        
    addressBook.addPerson(new Entry(name, street, town, postcode));
}


public Entry(String strName, String strStreet, String strTown, String strPostcode) throws BadDataException {
    name = strName;
    street = strStreet;
    town = strTown;
    postcode = strPostcode;
    try {
       checkDetails();
    } catch(Exception ex) {
       throw new BadDataException(ex);
    }
}

private static class BadDataException extends Exception {

    public BadDataException(final Throwable cause) {
         super(cause);
    }
}

Why not put your entry addition in a try-catch ? 为什么不将try-catch添加的条目添加进去?

try{
    Entry entry = new Entry(name, street, town, postcode);
    addressBook.addPerson(entry);
}catch(Exception e){
    // Entry is not added into the List.
    // do something and make the user re-enter the details.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM