Modify method to return a string?

0

Good, Veran I have this code

 private static void Perm2(String[] elem, String act, int n, int r) {
    if (n == 0) {
        System.out.println(act);
    } else {
        for (int i = 0; i < r; i++) {
            if (!act.contains(elem[i])) { // Controla que no haya repeticiones
                Perm2(elem, act + elem[i] + ", ", n - 1, r);
            }
        }
    }
}

and as you will realize it prints all the results in console, what I would like is to be able to save everything in a single String to be able to use that String at my convenience if possible or at least to print it in a JOptionpanel if it is possible clear.

Thank you in advance

    
asked by Diego Montiel 04.12.2017 в 08:07
source

1 answer

1

You say that it prints all the values to you by console but it will only paint you when n == 0 is true. In any case, if what you want is that instead of being printed on the screen, store it in a string, you can do it like this:

string result ="";  //Aquí guardaremos el resultado

private static void Perm2(String[] elem, String act, int n, int r) {
if (n == 0) {
    result = result + act; //Vamos concatenando los resultados
} else {
    for (int i = 0; i < r; i++) {
        if (!act.contains(elem[i])) { // Controla que no haya repeticiones
            Perm2(elem, act + elem[i] + ", ", n - 1, r);
        }
    }
}
}

We already have the result in result, we can print it or use it from another site.

    
answered by 04.12.2017 / 08:17
source