Prev Button out of bounds


#1

Just wondering, is there a better way to keep in bounds in the array index then
the if else statement, sorry- been awhile with Java

mBackButton = (Button)findViewById(R.id.back_button);
mBackButton.setOnClickListener(new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
			
			if(mCurrentIndex>=0)
			{
			       updateQuestion();
			}
			else
                            {
				mCurrentIndex=0;
				updateQuestion();
			}
		}
	});

#2

Most Java programmers prefer using the modulo operator, %, also known as the remainder operator:

5 % 4 == 1
6 % 4 == 2
9 % 4 == 1
-5 % 4 == -1

#3

What about:

@Override
public void onClick(View v) {
	int length = mQuestionBank.length;
	mCurrentIndex = (mCurrentIndex + (length-1)) % length;
	updateQuestion();

Keep that index nice and positive.


#4

That is a much better response than mine.

(I should try reading the code you guys post sometimes.)


#5

Hello everybody,
Great solution. I was wondering if it would be more efficient if I implemented the solution like this:

@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + (mQuestionBank.length - 1)) % mQuestionBank.length;
updateQuestion();

} //end onClick method

Thanks in advance.


#6

Nope! Local variables cost nothing.


#7

Thanks Phillips.


#8

This seems to work for me. Can anyone see any limitations to this other than the fact you need to know how many items are in the array? There are 5 questions in the array. Since 0 counts I just program the previous button to add 4 to the index on each click.

mPreviousButton = (Button) findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 4) % mQuestionBank.length;
updateQuestion();


#9

[quote=“Michael123”]This seems to work for me. Can anyone see any limitations to this other than the fact you need to know how many items are in the array? There are 5 questions in the array. Since 0 counts I just program the previous button to add 4 to the index on each click.

mPreviousButton = (Button) findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 4) % mQuestionBank.length;
updateQuestion();[/quote]

I was taught in another programming class to not hardcode a value like “4”.
If you add more items to the array, you have to remember to go into the code and change the 4 to something else.
One more place for possible error.


#10

Thanks WilsonG, can you (or someone) explain why mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length; won’t work? I at first thought just changing the “+” in the Next button to “-” in the Previous button would work, but it didn’t. So I realize I don’t really know what that code is doing. Can someone walk it through (i’m very much of a Java beginner).

The code that you wrote that does work is:

int length = mQuestionBank.length; mCurrentIndex = (mCurrentIndex + (length-1)) % length;

For instance, what is mQuestionBank.length referring to, and why do we use it? Is it simply where we are currently at in our array of questions? Or is that mCurrentIndex? Just trying to get a very straight forward and complete explanation of what is going on in that working line of code.

Thanks.


#11

[quote=“Nonimizu”]
what is mQuestionBank.length referring to[/quote]
the number of elements of mQuestionBank array

mCurrentIndex = (0 + 1 ) % 4 = 1
mCurrentIndex = (1 + 1 ) % 4 = 2
mCurrentIndex = (2 + 1 ) % 4 = 3
mCurrentIndex = (3 + 1 ) % 4 = 0


#12

I used a very simple approach:


// for NEXT button
	mCurrentIndex++;
	if (mCurrentIndex >= mQuestionBank.length)
	{
		mCurrentIndex = 0;
	}

// for PREV button
	mCurrentIndex--;
	if (mCurrentIndex < 0)
    	{
		mCurrentIndex = mQuestionBank.length - 1;
	}

This gets rid of the expensive modulos operator on every single button press, and replaces it with a easier to understand check and correction.

Increment or decrement the current index counter, and if the result is out of bounds (less than zero or equal or greater to the length of array) then loop back to other end of array.

All this should essentially be put into the model, and the activity should just request the model to move to next or previous question and leave the setting of the index to the model.


#13

[quote=“Nonimizu”]Thanks WilsonG, can you (or someone) explain why mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length; won’t work? I at first thought just changing the “+” in the Next button to “-” in the Previous button would work, but it didn’t. So I realize I don’t really know what that code is doing. Can someone walk it through (i’m very much of a Java beginner).

The code that you wrote that does work is:

int length = mQuestionBank.length; mCurrentIndex = (mCurrentIndex + (length-1)) % length;

For instance, what is mQuestionBank.length referring to, and why do we use it? Is it simply where we are currently at in our array of questions? Or is that mCurrentIndex? Just trying to get a very straight forward and complete explanation of what is going on in that working line of code.

Thanks.[/quote]

Keep in mind what we are trying to achieve.
If there is 1 question, mCurrentIndex only takes on the value of 0
If there are 2 questions, mCurrentIndex only takes on the value of 0 or 1
If there are 3 questions, mCurrentIndex only takes on the value of 0,1, or 2
If there are 4 questions, mCurrentIndex only takes on the value of 0,1,2 or 3
If there are 5 questions, mCurrentIndex only takes on the value of 0,1,2,3 or 4
If there are 6 questions, mCurrentIndex only takes on the value of 0,1,2,3,4 or 5
etc.


#14

I get an error message when I directly push the prev-button when starting my app in AVD. here is the code, is it something wrong with it??
I handle exceptions because the program complained about that earlier but I think it is unnecessary…

[code]mPreviousButton.setOnClickListener(new View.OnClickListener(){

    	public void onClick(View v) {
    		try{
    		mCurrentIndex = (mCurrentIndex - 1); // % mQuestionBank.length;
    		if(mCurrentIndex < 0){
    			mCurrentIndex = (mQuestionBank.length - 1);
    			System.out.println(mCurrentIndex);
    		}
    		}catch(Exception e){
    			System.out.println(mCurrentIndex);
    			
    		}
    		updateQuestion();[/code]