2014-10-13 3 views
0

큰 루프가 있는데 여기에는 내부의 모든 사람들에게 대량 텍스트 메시지를 모방하는 메소드가 있습니다. 주소록. 이 루프는 성공적으로 호출이 시작된 후 전송 된 응답을 포함하는 전자 메일을 검사합니다. 내 문제는 1 대 1 순으로 답장을 확인하고 다음 줄에있는 다른 답장이 수신 될 때까지 다른 답장을 확인하지 않는다는 것입니다.조건이 충족되지 않으면 for 루프의 다음 객체로 건너 뜁니다.

이 루프를 변경하여 모든 연락처가 회신하거나 응용 프로그램이 종료 될 때까지 루프의 모든 연락처를 확인하려면 어떻게해야합니까?

  • 이 난 접촉에 응답 할 때까지 메일을 확인하기 위해 계속 : 나는 잘 understoond 경우

    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 --; 
         } 
        }    
    } 
    
  • +0

    단지 MailReader.searchForPhone (con.getPhoneNo()) == 사실 내가 할 수있는 MailReader.searchForPhone (con.getPhoneNo()) –

    +0

    로 대체 말하는 ':이 경우에는이 방법으로 코드를 수정해야 어떤 것이 바깥 고리의 사용인지 이해하지 못한다. 각 n 요소에 대해 n 번씩 주소록을 확인하는 이유는 무엇입니까? – Panciz

    +0

    "내 문제는 1 대 1로 답장을 확인할 것이고, 다음 답장이 수신 될 때까지는 다른 답장을 확인하지 않을 것입니다."하나 이상의 메시지를 체크하고 싶다고 말하려고합니까? 우편물 상자에있는 시간에 parallell? – Panciz

    답변

    1

    이 같은 일을하는지 : 당신의 ADRESS 책에있는 모든 연락처로 문의

    1. 가 않습니다 가 도착하지 않았습니다. (에 의해 순서 1에서 응답에 대한 확인)

    하지만 당신은 주위에 다른 방법으로 원하는 : 응답을 보내

  • 확인 :

    1. 하는 응답이 도착할 때까지 메일을 확인하기 위해 계속을 테이블을 갱신하십시오.

    당신은 간단한 이것을 달성 할 수 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 객체는 테이블 구조를 닮은하지 않는 경우, 테이블에서 직접 전화 번호를 얻을 수 있습니다 (채워져 있다고 가정).


    편집 아마 내 설명은 충분하지 않았다.

    귀하의 수정 사항에 따라 필요에 따라 맵 구조가 변경되었지만 쓸모 없게 만들었습니다.

    Map은 키 - 값 쌍을 보유하는 데 사용되는 구조입니다. 귀하의 경우 (내 제안) 문자열과 정수를 개최하는 데 사용됩니다. 문자열은 전화 번호를 나타내지 만 정수는 테이블에서의 위치를 ​​나타냅니다. 우리가 같은 테이블 그래서 만약 :

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

    을 우리는 값이 포함 된지도합니다 : 우리가 Map.get("7654321") ("7654321" 키입니다) 호출 할 때, 이런 식으로

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

    우리가 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); 
        } 
    } 
    
  • +0

    while 루프는 업데이트 할 연락처를 알 수 없으므로이 메서드는 실제로 작동하지 않습니다. – Andigx

    +0

    'v'는 연락처의 목록이라고 가정하고 테이블 모델과 일치하고 그렇다면 업데이트 할 연락처를 알 것입니다. 첫 번째'for' 루프에서'reply'에 연락처의 인덱스'j'에 대한 참조를 저장하고'model.setValueAt (MailReader.getReply (phoneToCheck), replies.get (phoneToCheck) , 3); ' – Narmer

    +0

    그렇지 않으면 내 편집을 참조하십시오. – Narmer

    관련 문제