Java iterator accumulates

1

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);

            }
        }
    }
}
    
asked by Duban Zuluaga 21.03.2018 в 18:26
source

2 answers

2

I assume that A is to add, M is to calculate the sum and E is to finish:

What you need is to reset variable s after displaying it on the screen:

    if (n.equals("M")) {
        y = entrada.nextInt();
        iterador = lista.listIterator(); //<-- Inicializas el iterador
        s=0; // <-- inicializas el acumulador
        while (iterador.hasNext()) {
            t = iterador.next();
            if (t % y == 0) {
                s += t;

            }

        }
        System.out.println(s);

    }
    
answered by 21.03.2018 в 19:05
1

First, according to your input string, you are making the first time the list arrives on the line: if (n.equals ("M")) The list contains the values [1,2,3,4] and the value of t is 1 so the result of the first execution of the System.out.println (s); It's worth the sum of 1 + 2 + 3 + 4 - 10.

when collecting the next value and being another M, a road to enter with the same list LinkedList list = [1,2,3,4] but this time you take out those that modulo with 2 is 0, so you will add to the values that meet this. [2 and 4] Since you did not reinitiate s = 10 + 2 + 4.

To the next pass, the list remains the same LinkedList list = [1,2,3,4] this time we are going to execute the get the divisibles of 3, so only the [3] appears. having not restarted s: s = 16 + 3

now you will add the 5 to the list, because it is preceded by the letter A.  LinkedList list = [1,2,3,4,5]

and then you want to calculate which is divisible by 6, logically none. so there is nothing added to s.

but prints the value of s again on the screen. s = 19.

you have to check: 1st, if you want to calculate the list and then the divisors in the same entry, you would divide them into two differencing Arrays:

    public static void main(final String[] args) {
    LinkedList<Integer> lista = new LinkedList<>();
    Iterator<Integer> iterador;
    int x, y, s = 0, t;
    List<Integer> divisores = new ArrayList<Integer>();
    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);
        } else if (n.equals("M")) {
            divisores.add(entrada.nextInt());
        }

    }
    for (Integer integer : divisores) {
        s = 0;
        iterador = lista.listIterator();
        while (iterador.hasNext()) {
            t = iterador.next();
            if (t % integer == 0) {
                s += t;
            }
        }
        System.out.println(s);
    }
}

Notice that we have taken the tour of the values: on the one hand the list values, with [1,2,3,4,5] and on the other the list of divisors that will contain [1,2,3,6]

so it will give you 4 exits, the first to evaluate with 1 -> 15, when evaluating with 2 -> 6, and when evaluating with 3 -> 3, the last exit is to end with 6 -> 0.

If you need to adjust, I think that debug mode will be simple to your needs already.

Greetings.

    
answered by 22.03.2018 в 15:54