Why doesn’t the app come off the white screen and make no mistakes?

Asked

Viewed 596 times

0

I’m making a very basic app with a screen that simulates a Bingo card.

The problem is this, when I run the code, without checking repeated numbers (because in a bingo card there can be no repeated numbers), the program runs smoothly:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Random nRand = new Random();
            ArrayList<View> btn;
            int[] sorteados = new int[25];
            View cartela = findViewById(R.id.CartelaMain); //CartelaMain é o layout principal onde estão os 25 botões e um TextView
            btn = cartela.getTouchables();
    
            for(int cont = 0;cont < btn.size();cont++){
                int numero = nRand.nextInt(99) + 1; //Definindo o numero randômico
                Button botao = (Button) btn.get(contA); //Instanciando o botão
                String txt1 = Integer.toString(numero); //Passando de int para String
                botao.setText(txt1); //Set texto do botao
            }
        }

Now, when I apply the check:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Random nRand = new Random();
            ArrayList<View> btn;
            int[] sorteados = new int[25];
            View cartela = findViewById(R.id.CartelaMain); //CartelaMain é o layout principal onde estão os 25 botões e um TextView
            btn = cartela.getTouchables();
    
            for (int contA = 0; contA < btn.size(); contA++) { //Este método gera os numeros randomicametne e verifica por repetição dos numeros sorteados 
                int numero = nRand.nextInt(99) + 1;
                Boolean continuar = true;
                do {
                    Boolean sair = true;
                    for (int contB = 0; contB < btn.size(); contB++) {
                        int aux = sorteados[contB];
                        if(aux == numero){
                            numero = nRand.nextInt(99) + 1;
                            continuar = !sair;
                            contB = contB - 1;
                        }
                   }
    
                } while (!continuar) ;
    
                sorteados[contA] = numero; //Armazenamento do numero no Array
                Button botao = (Button) btn.get(contA); //Instanciando o botão
                String txt1 = Integer.toString(numero); //Passando de int para String
                botao.setText(txt1); //Set texto do botão
    
            }
    
        }

The app just doesn’t leave the white screen. There are no record of compilation errors on the part of Android Studio and I’ve also debugged some 4 times.


Acebei finding the solution to the above situation, there was an infinite loop loop loop:

int numero = nRand.nextInt(99) + 1;
            Boolean continuar = true;
            do {
                Boolean sair = true;
                for (int contB = 0; contB < btn.size(); contB++) {
                    int aux = sorteados[contB];
                    if(aux == numero){
                        numero = nRand.nextInt(99) + 1;
                        continuar = !sair;
                        contB = contB - 1;
                    }
               }

            } while (!continuar) ;

The problem was in an infinite loop of the/while method (which is totally unnecessary given that the for itself can prevent the output of a repeated number) which from the moment the check of that positive, two errors occurred:

1º - continue = ! exit; -> Because of this command, the do/while continued forever.

2º - contB = contB - 1; ->When declared this way, the loop created for verification was not effective. Ex:

for(cont = 0;cont < 5; cont++){ //Suponde que este é o terceiro ciclo cont = 3
    cont = cont - 1; //Implica que o cont vai voltar uma casa somente e não que ele vai começar do zero.
}

Fix it, the code goes like this:

public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Random nRand = new Random();
            ArrayList<View> btn;
            int[] sorteados = new int[25];
            View cartela = findViewById(R.id.CartelaMain); //CartelaMain é o layout principal onde estão os 25 botões e um TextView
            btn = cartela.getTouchables();
    
            for (int contA = 0; contA < btn.size(); contA++) { //Este método gera os numeros randomicametne e verifica por repetição dos numeros sorteados 

                for (int contB = 0; contB < btn.size(); contB++) {
                    int aux = sorteados[contB];
                    if(aux == numero){
                        numero = nRand.nextInt(99) + 1;
                        contB = -1;
                    }
               }
    
                sorteados[contA] = numero; //Armazenamento do numero no Array
                Button botao = (Button) btn.get(contA); //Instanciando o botão
                String txt1 = Integer.toString(numero); //Passando de int para String
                botao.setText(txt1); //Set texto do botão
    
            }
    
        }

RESOLVED

  • Please, could someone help me in this situation ?? Thank you :)

1 answer

0

for (int contA = 0; contA < btn.size(); contA++) {
        int numero = nRand.nextInt(99) + 1;
        Boolean continuar = true;
        for (int contB = 0; contB < btn.size(); contB++) {
             int aux = sorteados[contB];
             if(aux == numero){
                continuar = false;
                contA -= 1;
                break;
             }
        }
        if(continuar == true){
           sorteados[contA] = numero;
           Button botao = (Button) btn.get(contA);
           String txt1 = Integer.toString(numero);
           botao.setText(txt1);
        }
    }

I haven’t tested the code, but here’s a function that should do what you want, if you have any questions or don’t understand something says.

Like I said, I didn’t test the code, and if there’s a mistake, it says, I try to fix it.

I don’t think this is the best way to handle this, but I think it will work, good luck!

I think you could use the ArrayUtils.contains(array, key); - never used but it is a matter of trying :) I will do the code here with this solution, try and then tell if it worked or not.

for (int contA = 0; contA < btn.size(); contA++) {
        int numero = nRand.nextInt(99) + 1;
        if(ArrayUtils.contains(btn, numero);){
           sorteados[contA] = numero;
           Button botao = (Button) btn.get(contA);
           String txt1 = Integer.toString(numero);
           botao.setText(txt1);
        }else{
           contA -= 1;
        }
    }

I repeat, I have not tested any of these codes, and there may be some errors, if any and if you cannot solve it say.

  • I tried to implement the idea, but Android Studio does not have this method Arrayutils.contains. The funny thing is, the "error" only occurs when I make it perform the repetition method. I’ll wait for your test. Thank you.

Browser other questions tagged

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