How to check whether String[] Random Text is true or not

Asked

Viewed 103 times

2

I’m creating a game at Android Studio and I need to compare if the text on the screen is that or not, to then present the tip.

Ps: When using the equals get everything true by showing the message always, which is not what I want.

Private String[] obj = { bola, sapato, caderno }

Private String[] dica = { redondo, preto, folhas }

novoobj.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            Random randomico = new Random();
            int objaleatorio = randomico.nextInt(perguntas.length);
            textoobj.setText(obj[objaleatorio]);

 novadica.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            Random randomico = new Random();
            int dicaaleatoria = randomico.nextInt(dica.length);
            textodica.setText(dica[dicaaleatoria]);

Here comes the if that is not working. I want only if the obj position apart 0, he throws the hint. But in this case below throws with any value I put. I have tried to put the text too, but gave error in Java.

        if (!(obj.equals(0)){
            textodica.setText(dica[dicaaleatoria]);

That is, what I want, if text on the screen is ball for example then write the random hint. Round, for example.

  • You can share the entire class that is this stretch ?

2 answers

1

First thing you’ll need is to use the import java.util.Arrays , just below has a function that checks a string of the obj array and if it exists it goes in the hint array to look for the right hint. I put the string "notebook" as an example but you can use it as a function parameter. You can replace your if call with the search function();

import java.util.Arrays;

private String[] obj = { "bola", "sapato", "caderno"};
private String[] dica = { "redondo", "preto", "folhas" };

private  String buscarDica(){
    int index = Arrays.asList(obj).indexOf("cardeno");
    if (index > 0 && index < dica.length)
        return dica[index];
    else 
        return "Não tem dicas";
}
  • Fabio, Good afternoon. I’m new to Java, I don’t quite understand what you mean by validate. This Cód of yours does what?. Is it just me putting the if in front? Usually we use equals, but I tried and it’s no use. I’m crazy. Rsrs

  • Good afternoon, you need to do two things for the code to work, the first is to import the Arrays class more information about it here https://Developer.android.com/Reference/java/util/Arrays the second thing is to use the snippet of code within which I put in the answer inside your IF, it the index function will return the first element of your array in the case of a String, in which you can use the equals, I’ll try to adjust the code to improve.

  • I’m going to make some adjustments to the answers to get better, as soon as I have a little time, I don’t think it’s very clear!

  • Fabio, as I understand it, you want me to create a public Class Array, but the validation is within the onClick Class, I do not know if I would accept 1 class in another. The idea is that if clicking the Array word is == x, then show on the screen the y tip.

  • Frank, I really appreciate the help, this section of Cód. yours helped me, but I believe what I want to do is not possible in java by what I saw on some forums. I guess just changing the whole Cód to work. Getting your ex Cód: I would have to put what the int index received the value or the róprio index, inside the onClick tip event.

0


if (obj[0].equals(textoobj.getText())){
    textodica.setText(dica[dicaaleatoria]);

I managed to solve it that way. Stay here, in case someone needs.

Browser other questions tagged

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