简体   繁体   English

如果不满足条件,我如何跳至for循环中的下一个对象

[英]How do I skip to the next object in a for loop if a condition isn't met

I've got a big loop here that sits inside of a method that imitates a mass text message to all of the people inside of my address book. 我在这里有一个很大的循环,它位于一种方法中,该方法向我的地址簿中的所有人员模仿大量短信。 This loop(successfully) checks an email that contains any replies that are sent after the recall has been initiated. 此循环(成功)检查一封电子邮件,其中包含在发起召回后发送的所有答复。 My problem is that it will check for replies in order 1 by 1, and it won't check for any other replies until the next one in line is received. 我的问题是它将按1的顺序检查答复,并且直到收到下一行答复时才检查其他答复。

How can I change this loop to check all of the contacts in a loop until all of them have replied or the application is terminated? 如何更改此循环以检查循环中的所有联系人,直到所有联系人都已答复或应用程序终止为止?

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 --;
        }
    }               
}

If I understoond well you're doing like this: 如果我了解得很好,那么您的操作是这样的:

  1. For every contact in your adress book do: 对于地址簿中的每个联系人,请执行以下操作:
  2. Continue to check the mail until the reply for the contact I'm checking hasn't arrived. 继续检查邮件,直到没有收到我正在检查的联系人的回复。 ( check for replies in order 1 by 1 ) 按1的顺序检查答复

But you want the other way around: 但是您想要相反的方法:

  1. Continue to check the mail until a reply arrives: 继续检查邮件,直到收到答复:
  2. Check who sent the reply and update the table. 检查谁发送了回复并更新表格。

You can achieve this with a simple while loop and a lookup map: 您可以通过一个简单的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);
    }
}

EDIT 编辑

If your v object doesn't resemble your table structure, you can get the phone numbers directly from the table (assuming it's filled): 如果您的v对象与您的表结构不同,则可以直接从表中获取电话号码(假设已填充):

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

EDIT 2 编辑2

Maybe my explanation wasn't good enough. 也许我的解释不够好。

In your modification you altered the Map structure for your needs, but you make it useless. 在修改中,您根据需要更改了Map结构,但使它毫无用处。

A Map is a structure used to hold a Key-Value pair. 映射是用于保存键值对的结构。 In your case (my suggestion) is used to hold a String and an Integer. 在您的情况下(我的建议)用于保存String和Integer。 The string represents the phone number, while the integer represent its position in the table. 字符串代表电话号码,而整数代表其在表格中的位置。 So if we have a table like: 因此,如果我们有一个像这样的表:

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

we want a Map which contains the values: 我们想要一个包含值的Map:

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

In this way, when we call Map.get("7654321") ( "7654321" is the key) we will get 2 which is the row in the table for the phone number. 这样,当我们调用Map.get("7654321")"7654321"是键)时,我们将获得2 ,这是电话号码表中的行。

Now your code: 现在您的代码:

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
        }
        }

    }

}

The only meaning that I could find for you to use an Array is that the mail contains more than one phone number at a time. 我可以找到的使用数组的唯一含义是,邮件一次包含多个电话号码。 In this case you have to modify the code in this way: 在这种情况下,您必须以这种方式修改代码:

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);
    }
}

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

相关问题 如果不满足特定条件,我如何终止 Java 程序? - How do I terminate a Java program if a certian condition isn't met? 如果不满足条件? - If condition isn't met? 不满足条件时,while循环不会中断 - While loop not breaking when its condition isn't met 如果不执行do while循环条件,如何打印消息 - How to print a message if the do while loop condition is not met 如果满足 if 循环内部条件,如何退出 for 循环 - How to exit a for loop if the if loop inside condition is met 如果满足特定条件,如何更改 Java 中字符串中的元音? - How do I change vowels in a string in Java if a certain condition is met? (Java)如果从未满足过for循环中if语句的条件,我该如何打印一条消息,指出“没有值满足此条件”? - (Java) If the condition for an if statement inside a for loop is never met, how do I print out a message saying “no values meet this criteria”? 如果满足条件,但跳至下一个条件 - IF Condition is Met but skipped to the next IF 为什么不满足条件后我的 Do While 循环不停止? - Why doesn't my Do While loop stop after the condition is not met? 当满足while条件时,为什么第一次do while循环没有结束? - Why doesn't the first do while loop end when the while condition is met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM