Previous Button Challenge Help Needed

#1

So I was able to implement the previous button and it works well but not on the first question.
If im on the second question and i press the previous button, it goes back to the first. (like it should)
If im on the third question, it goes back to the second. (like it should)
But if i’m on the first and press the button, the app just crashes.
Here is all relevent code:

[code]
import android.widget.ImageButton;
private ImageButton mPreviousButton;

	mPreviousButton = (ImageButton)findViewById(R.id.previous_button);
	mPreviousButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
			updateQuestion();
		}
	});[/code]

edit: scratch that… got my answer… but i still dont understand what mQuestionBank.length stands for…

#2

mQuestionBank.length represents the length of the mQuestionBank array. What you’re really doing by typing that is accessing a constant of mQuestionBank called length via the “dot” operator. All arrays in java also have this constant which is initialized when you create the array. So if I were to create a new array like this:

String[] blargle = new String[5];

This creates an array of type String called blargle, which has a length of 5 (indices 0 through 4)

Hope this clarifies that for you.

#3

[quote=“sparkimus”]mQuestionBank.length represents the length of the mQuestionBank array. What you’re really doing by typing that is accessing a constant of mQuestionBank called length via the “dot” operator. All arrays in java also have this constant which is initialized when you create the array. So if I were to create a new array like this:

String[] blargle = new String[5];

This creates an array of type String called blargle, which has a length of 5 (indices 0 through 4)

Hope this clarifies that for you.[/quote]
Thats a great explanation. Thanks.

#4

[quote=“trickedoutdavid”]So I was able to implement the previous button and it works well but not on the first question.
If im on the second question and i press the previous button, it goes back to the first. (like it should)
If im on the third question, it goes back to the second. (like it should)
But if i’m on the first and press the button, the app just crashes.
Here is all relevent code:

[code]
import android.widget.ImageButton;
private ImageButton mPreviousButton;

	mPreviousButton = (ImageButton)findViewById(R.id.previous_button);
	mPreviousButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
			updateQuestion();
		}
	});[/code]

edit: scratch that… got my answer… but i still dont understand what mQuestionBank.length stands for…[/quote]I am working on this challenge right now and my Next button keeps crashing…how did you fix this? I have pretty much the same code as you do.

#5

let me see it… post your code…

#6

This is what I have:

[code]
mPrevButton = (Button)findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();
}
});

    mNextButton = (Button)findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {
    	@Override
    	public void onClick(View v){
    		mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
    		updateQuestion();
    	}
    });
    
    updateQuestion();[/code]

Let me know if you need to see anything else

#7

The idea is to iterate through the questions stored in the array. This means that, when going backward, you need to go from the first question to the last question. In all other cases you can simply go from a given question to the preceding question. The variable to set in order to do this is mCurrentIndex. The first question has index 0 in the array, the last one has index 4, which is equal to array.length-1 (since the array has 5 questions and its index starts at 0). So you need to go from 0 to 4 and then back down again

The code setting the index can be:

if (mCurrentIndex == 0) mCurrentIndex = mQuestionBank.length-1;
else mCurrentIndex = mCurrentIndex -1;

Or using the tenary operator, condensed into one line:

mCurrentIndex = (mCurrentIndex == 0) ? mQuestionBank.length-1 : mCurrentIndex -1;

And then the whole thing would be:

		mPrevButton = (ImageButton)findViewById(R.id.prev_button);
		mPrevButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mCurrentIndex = (mCurrentIndex == 0) ? mQuestionBank.length-1 : mCurrentIndex -1;
				UpdateQuestion();
			}
		});
#8

You guys got it.

The problem here is that the Java modulo operator is (to me) a little irritating. When I say “x % 4”, I almost always want it to return 0, 1, 2, or 3. But Java can return -3, -2, or -1, too.

#9

or you can do this: