I do not know how to call a certain list item that enters my "for" in other functions

0

I'm trying to make a program that picks up a list of members that contains the name, email, and renewal day (type of day pay monthly), and when it's the day, send an email to him.

But I have trouble getting the name and email of that particular member who is in the day to send the email. I tried to use socio[i] (which is the name of the list), which is the number that entered for .

In my if , I am not able to compare day_of_month with date, date is integer and so it seems day_of_month is of another type.

Follow my code to see if it helps me understand my problem

 package trabalho2;

import java.util.ArrayList;
import java.util.Calendar;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class Main {

public static void main(String[] args) {
    ArrayList <Socios> socios = new ArrayList();
    //cadastrando os sócios
    Socios socios1 = new Socios("Mateus", "[email protected]", 11);
    Socios socios2 = new Socios("Eduardo Moya Simões", "[email protected]", 11);
//isso aqui é tipo colocando o q foi cadastrado na lista
    socios.add(socios1);
    socios.add(socios2);


    Calendar c = Calendar.getInstance();

    SimpleEmail email = new SimpleEmail();
    email.setSSLOnConnect(true);
    email.setHostName("smtp.googlemail.com");
    email.setSslSmtpPort("465");
    email.setAuthenticator( new DefaultAuthenticator( "[email protected]" , "nossasenha"));


    for (int i = 0; i < socios.size(); i++) {
        //isso só serve para vc ver como ta funcionando, depois tem q apagar
        System.out.println("Socio: " + socios.get(i));

        System.out.println(Calendar.DAY_OF_MONTH);
        if (c.get(Calendar.DAY_OF_MONTH) = socios(i).getData) {

            // função manda e-mail

        try {
          email.setFrom( "[email protected]", "Socio torcedor");

            email.setDebug(true);

          email.setSubject( "Pagamento fatura");
          email.setMsg("Salve dpoente\n tu tem q pagar sua mensalidade né jumento");
          email.addTo(socios.getEmail, socios.getNome);

          email.send();
          System.out.println("Email enviado para: " + socios.getEmail +" de " + socios.getNome);
     }catch (EmailException e) {
         e.printStackTrace();
    }
        }
    }
}

}
    
asked by anonymous 12.12.2016 / 05:02

2 answers

1

Your code has several problems. Among them:

Problems no if

The incident is in the following line:

if (c.get(Calendar.DAY_OF_MONTH) = socios(i).getData)

The problems are:

  • You are using operator = instead of == . The first one is used to flag, while the second is used for equality comparison.
  • You are using an invalid syntax to fetch items from socios . The correct thing is to use the ArrayList # get method. . Even you used it correctly to test if it was working ...
  • By name, I suppose socios(i).getData is a method call. The Java syntax requires the use of parentheses.

All together, I would stay:

if (c.get(Calendar.DAY_OF_MONTH) == socios.get(i).getData())

Using non-existent methods

In several places of the source, methods like:

socios.getEmail

The problems are:

  • The socios list has no such methods. First, you need to access an item in the list with Array # get.
  • Use of parentheses is mandatory for invocation of methods, as mentioned above.

Conclusions

These types of errors can be avoided with the use of a good IDE and especially with knowledge about the language.

I feel that a good study of Java would benefit you greatly, I recommend that you do so. The Oracle tutorials are off to a good start.

Even if you want to improve your code, look for a little gem called foreach.

    
12.12.2016 / 12:07
0

Apparently there are some errors in the code.

Your comparison is missing = and if getData is a method, there is missing parentheses () [.get(Calendar.DAY_OF_MONTH) == socios(i).getData()]

On the plus side, everything seems to be correct.

    
12.12.2016 / 11:55