Reputation: 23
I have started to work on app that has multiple choice questions, but when I try to run my app it says:
java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
I know what it says but I don't understand where is the problem.
here's my code:
TextView tvq;
InputStream is;
String[] question = new String[10];
ImageButton[] buttons = new ImageButton[4];
int[] check = {-1, -1 ,-1};
int[] answersid = {R.drawable.israelflag, R.drawable.spainflag, R.drawable.franceflag, R.drawable.greeceflag, R.drawable.egyptflag, R.drawable.unitedstatesflag, R.drawable.brazilflag, R.drawable.japanflag, R.drawable.turkeyflag, R.drawable.iraqflag};
int i, randombutton, correctanswerid ,a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia);
buttons[0] = (ImageButton) findViewById(R.id.im1);
buttons[0].setOnClickListener(this);
buttons[1] = (ImageButton) findViewById(R.id.im2);
buttons[1].setOnClickListener(this);
buttons[2] = (ImageButton) findViewById(R.id.im3);
buttons[2].setOnClickListener(this);
buttons[3] = (ImageButton) findViewById(R.id.im4);
buttons[3].setOnClickListener(this);
tvq = (TextView) findViewById(R.id.tvq);
try {
setQuestion();
} catch (IOException e) {
e.printStackTrace();
}
game();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_trivia, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void setQuestion() throws IOException {
int z = 0;
String st = "";
is = this.getResources().openRawResource(R.raw.questions);
InputStreamReader isr1 = new InputStreamReader(is);
BufferedReader br1 = new BufferedReader(isr1);
while ((st = br1.readLine()) != null) {
question[z] = st;
z++;
}
is.close();
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.im1) {
if (R.id.im1 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
if (id == R.id.im2) {
if (R.id.im2 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
if (id == R.id.im3) {
if (R.id.im3 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
if (id == R.id.im4) {
if ( R.id.im4 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
}
public void game()
{
i = (int) (8 * Math.random());
tvq.setText(question[i]);
randombutton = (int) (3 * Math.random());
buttons[randombutton].setImageResource(answersid[i]);
correctanswerid = buttons[randombutton].getId();
ImageButton temp = buttons[randombutton];
buttons[randombutton] = buttons[buttons.length-1];
for (int s = 0; s < buttons.length - 1; s++) {
a = (int) (9 * Math.random());
for (int k = 0; k < check.length ; k++) {
if (check[k] == a || a == i ) {
a = (int) (9 * Math.random());
while (check[k] == i || a == 1)
a = (int) (9 * Math.random());
}
}
check[s] = a;
buttons[s].setImageResource(answersid[a]);
}
}
}
here's my raw's file:
Israel?
Spain?
France?
Greece?
Egypt?
United States?
Brazil?
Japan?
Turkey?
Iraq?
Thanks for help! (sorry for my bad English)
Upvotes: 2
Views: 8565
Reputation:
The problem is with the question array.
String[] question = new String[10];
Here your value for z is exceeding 9, the max index of your question array.
while ((st = br1.readLine()) != null) {
question[z] = st;
z++;
}
You need to stop it going past 9. Like this:
while (((st = br1.readLine()) != null) && (z < 10)) {
Upvotes: 7