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

Asked

Viewed 86 times

0

I’m trying to make a program that picks up a list of partners that contain the name, email, and renewal day(like paying monthly fee), and when the day comes, send an email to it.

But I’m having trouble getting the name and email of that particular partner who is on the day of sending the email. I tried to use socio[i](which is the name of the list), which is the number you entered in the for.

In my if, I’m not being able to compare day_of_month with the date, date is integer and it seems to me day_of_month is of another kind.

Follow my code to see if it helps 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();
    }
        }
    }
}

}

2 answers

1

Your code has several problems. Among them:

Problems in the if

The incident is on the following line:

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

The problems are:

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

Everything together would be:

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

Use of non-existent methods

In various places of the source, methods such as:

socios.getEmail

The problems are:

  • The list socios does not have such methods. First, you need to access an item from the list with Array#get.
  • Use of parentheses is mandatory for method invocation, 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 on Java would benefit you a lot, I recommend that you do. The tutorials from Oracle are a good start.

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

  • Thank you, you helped me a lot but help me on the following:

  • @I’ve already tried to help?

  • Thank you, you helped me a lot but help me out with the following: "socios.getEmail The problems are: The socios list does not have such methods." I actually created another class called socios with the email, name, and date variables and added the gets and sets. About 'socios(i)', socios is a list, if you notice in the 'for (int i = 0; i < socios.size(); i++)' the intention is to pass through here all the lists, so 'socios' is the name and 'i' is the count;

  • when he enters this 'if' the intention is to get the list with that i; for example, no for passed the socios3 list, no if then when I say: 'if (c.get(Calendar.DAY_OF_MONTH) = socios(i).getData)' I am wanting to test the socios 3

  • I was writing, kkkkk

0

Apparently there are some errors in the code.

Your comparison is missing one = and whether the getData is a method, parentheses are missing () [.get(Calendar.DAY_OF_MONTH) == socios(i).getData()]

Everything else seems to be right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.