简体   繁体   中英

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.

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 )

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:

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):

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

EDIT 2

Maybe my explanation wasn't good enough.

In your modification you altered the Map structure for your needs, but you make it useless.

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. 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 = {[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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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