I want to make a program that adds elements type int to a list and with the help of an iterator add the elements that meet certain characteristics. The problem is that when I give several conditions, the results accumulate. Example: I add 1 2 3 4 5 to the list and I put two conditions, the first is that add those that are divisible by 2 and the second those that are divisible by 3; thus the result would be 6 in condition one and 3 in condition two. But with this error, I get 6 in the first and in the second 9 because the results accumulates them. This is my code, the example would be entering: A 1 A 2 A 3 A 4 M 1 M 2 M 3 TO 5 M 6 E
Note that the instructions are entered by keyboard using "M", "A" and "E"
import java.util.*;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> lista = new LinkedList<>();
Iterator<Integer> iterador;
int x, y, s = 0, t;
String n = "jjkb";
Scanner entrada = new Scanner(System.in);
while (!n.equals("E")) {
n = entrada.next();
if (n.equals("A")) {
x = entrada.nextInt();
lista.add(x);
}
if (n.equals("M")) {
y = entrada.nextInt();
iterador = lista.listIterator();
while (iterador.hasNext()) {
t = iterador.next();
if (t % y == 0) {
s += t;
}
}
System.out.println(s);
}
}
}
}