[ Do while, Handler and runnable -Android ]

I can't for the life of me get this do while loop running on a button click even with a runnable and handler. Can anyone please show me where I have made my mistake?

{//Class start

    Button roll;
    int numberOfDice;
    int diceCounter;
    EditText diceBox;
    EditText outputBox;
    String diceNum;
    Handler myHandler = new Handler();
    private boolean Running;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_roller_screen);
        roll = (Button) findViewById(R.id.roll_Button);
        diceBox = (EditText) findViewById(R.id.numberOfDiceBox);
        outputBox = (EditText) findViewById(R.id.outputBox);

        roll.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) 
            {
                myHandler.post(runner);
            }

        });


    }


    Runnable runner = new Runnable() {

        int dicecounter = 1;
        Random rand = new Random();

        public void run() {
            do{

                int dice_Value = rand.nextInt((6 - 1) + 1) - 1;
                diceNum = diceBox.getText().toString();
                numberOfDice = Integer.parseInt(diceNum);
                outputBox.setText("Dice"+dicecounter+dice_Value);
                dicecounter++;
                }while(diceCounter <= numberOfDice);    
            myHandler.post(this);
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.roller_screen, menu);
        return true;
    }
}

I can't figure out if it's my logic or if I'm using runners and handlers completely wrong.

Answer 1


This is almost certainly an endless loop problem: dicecounter starts at one so if you enter a value less than two into diceBox the do..while loop will never terminate. The counter should start at zero and you should validate the number of dice value entered by the user (including checking for number format exception, unless the edit control only allows digits?)

But in any case, why is there a do..while loop there anyway? All it does is overwrite the text in outputBox, so even if the loop was fixed all the user will see is the result of the last iteration of the loop.