Count integer occurrence in an Arraylist

Asked

Viewed 104 times

0

Good afternoon, I’m having a lot of trouble calculating the total number of student absences, I’m not able to implement this class somafalta, I would like a help. from now on.

import java.util.ArrayList;
import java.util.List;

public class Principal {
public static void main(String[] args) {
    ArrayList<Faltas> faltas = new ArrayList<>();
    faltas.add(new Faltas(111, 3, 15));
    faltas.add(new Faltas(111, 1, 29));
    faltas.add(new Faltas(222, 12, 3));
    faltas.add(new Faltas(222, 12, 4));
    faltas.add(new Faltas(222, 12, 5));
    System.out.println("Aluno reprovado"+falta.getMatricula);

}
public class Faltas {
    int matricula;
    int mes;
    int dia;
    int totalFaltas;
    ArrayList<Faltas> faltas = new ArrayList();
    public Faltas(int matricula, int mes, int dia) {
        this.matricula = matricula;
        this.mes = mes;
        this.dia = dia;
    }
    public void setMatricula(int matricula) {
         this.matricula = matricula;
    }
    public int getMatricula() {
         return matricula;
   }
    public void inserirDados(int matricula,int mes, int dia) {
        faltas.add(new Faltas(matricula,mes,dia));
    }
    void somaFaltas(int matricula,int dia) {// teria que somar faltas pegando o dia da falta, e assim que eu chamasse a classe Falta na main obteria a matricula e o total de faltas do aluno.
        int i=0;
        for (Faltas falt : faltas){
            if (falt.contains(matricula)==true && falt.contains(dia)==true){
                i++;
            }
     }
   }
}

1 answer

2


Below is a way to resolve this.

import java.util.ArrayList;
import java.util.List;

public class Principal {
  public static void main(String[] args) {
      ArrayList<Faltas> faltas = new ArrayList<>();
      faltas.add(new Faltas(111, 3, 15));
      faltas.add(new Faltas(111, 1, 29));
      faltas.add(new Faltas(222, 12, 3));
      faltas.add(new Faltas(222, 12, 4));
      faltas.add(new Faltas(222, 12, 5));

      System.out.println("Faltas"+somaFaltas(222,3, faltas));
  }

  public static int somaFaltas(int matricula,int dia, ArrayList<Faltas> faltas) {
      int i=0;
      for (Faltas falt : faltas){
          if (falt.matricula===matricula && falt.dia===dia){
              i++;
          }
      }

      return i;
  }

}

Browser other questions tagged

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