1

I want to complete a task, which assigns the variable randomButton a value. At the end, I want to check the value of randomButton If it is something I don't like, I want to re do the task where I assign randomButton a value.

I want to do this task:

        int i = rng.nextInt(4); //Find random button from between 1-4
        randomButton = buttons.get(i);
        buttonBackground = randomButton.getBackground();
        Log.v(TAG, "In the do");

and if

buttonBackground.equals(R.drawable.redcircle)

I want to try and do the task again. How do I achieve this...?

I have tried:

do {
            int i = rng.nextInt(4); //Find random button from between 1-4
            randomButton = buttons.get(i);
            buttonBackground = randomButton.getBackground();
            Log.v(TAG, "In the do");
        }while(!buttonBackground.equals(R.drawable.redcircle));

But that is an infinite loop. So how do I repeat a task if the value is something I don't like?

By the way, buttons is an arraylist with different image button objects. I want to see if the background of that imagebutton (randomButton) is redcircle. If it is, I want to go through the code again. If it isn't, thats great, and I can continue.

  • set the while to correct value (something that you don t like) and be sure that sometimes it can be something you like, hence now its never a R.drawable.redcircle – Petter Friberg Nov 28 '15 at 23:20
  • I think your problem lies in `buttonBackground.equals(R.drawable.redcircle)`. `R.drawable.redcircle` is an `int`. What do you want to do exactly? – m0skit0 Nov 28 '15 at 23:22
  • @PetterFriberg Yes, I am sure that buttonBackground can equal something other then the drawable redcircle. My arraylist of buttons `buttons` contains four buttons with different drawables...So it is sometimes redcircle... –  Nov 28 '15 at 23:22
  • You have ! before so it needs to be the int value of R.drawable.redcircle – Petter Friberg Nov 28 '15 at 23:24
  • @PetterFriberg Oh. I just wanted to check whether the background image is redcircle or not. Thats why I put !. To say not equal. What should I do instead? –  Nov 28 '15 at 23:25
  • @m0skit0 I want to see if the background of that imagebutton (randomButton) is redcircle. If it is, I want to go through the code again. If it isn't, thats great, and I can continue. –  Nov 28 '15 at 23:26
  • Remove the !... System.out.println the stuff that i need to know what is it.... – Petter Friberg Nov 28 '15 at 23:26
  • @PetterFriberg Well this is android, so I can't System.out.println, but I can log it. But, I'm trying to see if the background of that imagebutton (randomButton) is redcircle. If it is, I want to go through the code again. If it isn't, thats great, and I can continue –  Nov 28 '15 at 23:28
  • What class is buttonBackground?, what class is R.drawable.redcircle? – Petter Friberg Nov 28 '15 at 23:29
  • @PetterFriberg redcircle is an image stored in my drawable directory. ButtonBackground is the background of the random button which I pick out from my `buttons` array list –  Nov 28 '15 at 23:30
  • You should surely remove the NOT ! and then you need to figure out how you can determine if the background has the redcirlce image (log some), I don't think the current compare will work – Petter Friberg Nov 28 '15 at 23:33

1 Answers1

0

A do-while loop would be the correct way to to this. I do not know what R.drawable.redcircle means, is that a color?

You did something wrong in the while clause:

}while(!buttonBackground.equals(R.drawable.redcircle));

I should have posted this in the comment but i couldn't since i dont have 50 reputation yet.

Xyexs
  • 64
  • 5
  • For clarification: Everything in `R.drawable` is an Object of type `Drawable`. It can be anything like color, image, vector graphic, gradient or simply anything you can *draw* on the screen of a device. – GiantTree Nov 28 '15 at 23:33