java – Sort letters in a matrix, amount typed by the user

Question:

I'm having trouble here so that the user can type in how many letters of each he wants to be drawn. Example as the code below shows the letters char[] letras = new char[]{'S', 'C', 'M', 'A','L'}; . These letters are drawn in the matrix, but I want the user to type how many S , how many C , how many M and so on. My code is the one below…

public class exemplo {


    public static void main(String[] args) {

        int tamanho = 4; 

        char[] letras = new char[]{'S', 'C', 'M', 'A','L'};
        char matrix [][] = new char[tamanho][tamanho];


        Random random = new Random();
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {

                matrix[i][j] = letras[random.nextInt(letras.length)];

            }

        }
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {
                System.out.print("|" + matrix[i][j] + "|");
            }

       System.out.println("");
    }
    }
}

Answer:

Hi well I don't know if this is exactly what you were looking for but this was my solution,

First I created a list of characters to store all the possible letters to be drawn,

List<Character> base_sorteio = new ArrayList<>();

Then I made a loop to ask the user for the desired amount of each letter using the Scanner class, then I add to the character base the amount entered by the user for that particular character,

    int y = 0;
    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < letras.length; i++) {

        System.out.println("Informe a quantidade de '" + letras[i] + "'");

        int k = sc.nextInt();
        for (int f = 0; f < k; f++) {
            base_sorteio.add(letras[i]);
        }
    }

After entering the number of each letter, the character list will store something like:

[S, C, C, C, C, M, M, M, M, M, A, A, A, L, L, L]

In this case, I typed 1S,4C,5M,3A and 3L. Then I shuffle the list,

Collections.shuffle(base_sorteio); 

getting like this:

[M, M, C, C, A, A, L, C, L, M, C, M, S, A, L, M]

After that, just add this list to the matrix:

    y = 0;
    for (int i = 0; i < tamanho; i++) {
        for (int j = 0; j < tamanho; j++) {
            matrix[i][j] = base_sorteio.get(y);
            y++;
        }
    }

Below is the full code:

import java.util.*;

public class exemplo {

    public static void main(String[] args) {

        int tamanho = 4;

        char[] letras = new char[]{'S', 'C', 'M', 'A', 'L'};
        char matrix[][] = new char[tamanho][tamanho];

        List<Character> base_sorteio = new ArrayList<>();

        int y = 0;
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < letras.length; i++) {

            System.out.println("Informe a quantidade de '" + letras[i] + "'");

            int k = sc.nextInt();
            for (int f = 0; f < k; f++) {
                base_sorteio.add(letras[i]);
            }
        }

        Collections.shuffle(base_sorteio);

        y = 0;
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {
                matrix[i][j] = base_sorteio.get(y);
                y++;
            }
        }
        System.out.println("");
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {
                System.out.print("|" + matrix[i][j] + "|");
            }

            System.out.println("");
        }
    }
}
Scroll to Top