【问题标题】:How do I skip to the next object in a for loop if a condition isn't met如果不满足条件,如何跳到 for 循环中的下一个对象
【发布时间】:2014-10-13 12:25:08
【问题描述】:

我在这里有一个大循环,它位于一个方法内部,该方法模仿大量文本消息给我通讯录中的所有人。此循环(成功)检查一封电子邮件,其中包含在发起召回后发送的任何回复。我的问题是它将按 1 到 1 的顺序检查回复,并且在收到下一个在线回复之前不会检查任何其他回复。

如何更改此循环以检查循环中的所有联系人,直到所有联系人都回复或终止应用程序?

Map<String, Integer> replies = new HashMap<String, Integer>();

for (int j = 0; j < v.size(); j++){
    replies.put(((Member)v.elementAt(j)).getPhoneNo(), j); //Save a reference to the phone number AND its index in "v".
}
String host = "pop.gmail.com";
String mailStoreType = "pop3";
String username = "********@gmail.com";
String password = "********";
String[] phoneToCheck = new String[v.size()];
int phoneCheck = phoneToCheck.length;

while(phoneCheck > 0){

    MailReader.check(host, mailStoreType, username, password); 

    for(int i = 0; i < v.size(); i++) {
        con = (Member) v.elementAt(i);
        phoneToCheck[i] = con.getPhoneNo();

        if (replies.containsKey(phoneToCheck)) {
            newFrame.addNotify();
            System.out.println("IT WORKED");

            model.setValueAt(MailReader.getReply(phoneToCheck[i]), 
                                                 replies.get(phoneToCheck[i]), 3);
            model.fireTableDataChanged();

            replies.remove(phoneToCheck);

            phoneCheck --;
        }
    }               
}

【问题讨论】:

  • 只是说 MailReader.searchForPhone(con.getPhoneNo()) == true 替换为 MailReader.searchForPhone(con.getPhoneNo())
  • 看不懂哪个是外循环的用处。为什么你要为每个 n 元素检查 n 次通讯录?
  • " 我的问题是它会按 1 到 1 的顺序检查回复,并且在收到下一个在线回复之前不会检查任何其他回复。" 你是想说您想同时检查多个邮箱吗?
  • 不,我的 for 循环不会检查我的收件箱中的任何回复,除了包含我通讯录中第一个联系人的电话号码的回复。因此,如果我通讯录中的第一个人没有发送回复,我的循环将不会检查电子邮件以获取下一个回复。

标签: java swing for-loop while-loop


【解决方案1】:

如果我理解得很好,你会这样做:

  1. 对于地址簿中的每个联系人,请执行以下操作:
  2. 继续检查邮件,直到我正在检查的联系人回复 还没到。 (按 1 到 1 的顺序检查回复

但你想反过来:

  1. 继续检查邮件直到收到回复:
  2. 检查谁发送了回复并更新表格。

您可以通过一个简单的 while 循环和一个查找图来实现这一点:

Map<String, Integer> replies = new HashMap<String, Integer>();
for (int j = 0; j < v.size(); j++)
    replies.put(((Member)v.getElementAt(j)).getPhoneNo(), j); //Save a reference to the phone number AND its index in "v".

MailReader mR2 = new MailReader();
String host = "pop.gmail.com";
String mailStoreType = "pop3";
String username = "********@gmail.com";
String password = "********";

while(!replies.isEmpty()){
    MailReader.check(host, mailStoreType, username, password); //Is this correct? Why are you using a static method? What's the purpose of mR2?
    String phoneToCheck = MailReader.getPhone(); //You have to implement this if it's not present
    if (replies.containsKey(phoneToCheck)) {
        newFrame.addNotify();
        System.out.println("IT WORKED");

        model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
        model.fireTableDataChanged();
        replies.remove(phoneToCheck);
    }
}

编辑

如果您的 v 对象与您的表结构不同,您可以直接从表中获取电话号码(假设已填满):

for(int i = 0; i < model.getRowCount(); i++){
    replies.put((String)model.getValueAt(i,3), i); //Assuming 3 is phone number column
}

编辑 2

也许我的解释不够好。

在您的修改中,您根据需要更改了 Map 结构,但您使其无用。

Map 是用于保存键值对的结构。在您的情况下(我的建议)用于保存字符串和整数。字符串代表电话号码,而整数代表其在表格中的位置。因此,如果我们有这样的表:

------------+---------------
|     1     |    1234567   |
------------+---------------
|     2     |    7654321   |
------------+---------------

我们想要一个包含值的地图:

Map = {[12345678, 1],[7654321, 2]}

这样,当我们调用Map.get("7654321")"7654321" 是键)时,我们将得到2,这是表格中电话号码的行。

现在你的代码:

Map<String, Integer> replies = new HashMap<String, Integer>();

//First thing first, fill the map with the phone number and row number 
//as described above.
for (int j = 0; j < v.size(); j++)
    replies.put(((Member)v.elementAt(j)).getPhoneNo(), j); 
    //                                       ^         ^   
    //                                phone number     row

    //[omissed for brevity]

    //This is wrong, we want to check if the map has still phone numbers in it
    //while(phoneCheck >0){
    while(!replies.isEmpty()){ // This means "while replies is not(!) empty"

        //I don't know how MailReader is implemented, but I assume that this call
        //checks if a new mail has arrived.
        MailReader.check(host, mailStoreType, username, password);

        //Now comes the IMPORTANT part. You have to retrieve the phone number
        //from the INCOMING MAIL! So you can understand who replied.

        //Again, I don't know how MailReader is implemented, I assumed that a getPhone()
        //method is available.
        //Is such method doesn't exist you have to CREATE IT. It must retrieve the 
        //phone number contained in the mail.

        String phoneToCheck = MailReader.getPhone(); //Get the phone from the mail.

        //This is completely useless.
        /** 
        for(int i = 0; i<v.size(); i++) {
            con = (Member) v.elementAt(i);
            phoneToCheck[i] = con.getPhoneNo();
        **/
        if (replies.containsKey(phoneToCheck)) {
            newFrame.addNotify();
            System.out.println("IT WORKED");

            //Where the hell does i came from?? It belongs to the for loop you FINISHED earlier!!
            //model.setValueAt(MailReader.getReply(phoneToCheck[i]), replies.get(phoneToCheck[i]), 3);
            model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
            model.fireTableDataChanged();
            replies.remove(phoneToCheck);

            //phoneCheck --; Useless
        }
        }

    }

}

我可以为您找到使用数组的唯一含义是邮件一次包含多个电话号码。在这种情况下,您必须以这种方式修改代码:

String phonesToCheck[] = MailReader.getPhones(); //Get the phones from the mail. BEWARE!! This is an array!
for(String phoneToCheck: phonesToCheck){
    if (replies.containsKey(phoneToCheck)) {
        newFrame.addNotify();
        System.out.println("IT WORKED");

        model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
        model.fireTableDataChanged();
        replies.remove(phoneToCheck);
    }
}

【讨论】:

  • 该方法不会真正起作用,因为 while 循环不知道要更新哪个联系人。
  • 如果v(我假设是您的联系人列表)与表格模型一致,那么是的,它将知道要更新哪个联系人。在第一个for 循环中,您保存replies 中联系人的索引j 的引用,并使用它来更新model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3); 中的表
  • 我实现了它,但它不起作用。我对它进行了大量修改,看看是否可以让它做我需要它做的事情,但它似乎无法识别何时找到电话号码。我会将来源添加到我的主要帖子中。
  • 你没有像我那样实现。你完全错过了地图的定义...如果phoneToCheck 是一个数组,replies.containsKey(phoneToCheck) 毫无意义...另外,从邮件中获取电话的String phoneToCheck = MailReader.getPhone(); 在哪里?在您的修改中,您根本没有提及它。
  • @Remixt 真的,那个数组是从哪里来的?你为什么用它?你已经有了地图……如果你不知道你在做什么,请不要修改代码。
猜你喜欢
  • 1970-01-01
  • 2017-10-05
  • 2021-08-22
  • 2019-09-03
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 2021-05-15
  • 2019-11-23
相关资源
最近更新 更多