0

Road rules app

I have an activity where questions and answers will be displayed depending on the ticket number(20 questions and 5x20 answers).Questions and answers contained in String resouses and sorted. My code is very shi**y, I want to simplify by loops like in case with questions [q] but with 2 variables. Help me please.

https://play.google.com/store/apps/details?id=ua.in.sil.cryzalit.mybilets

number = intent.getStringExtra("number");


  for( int i = 1; i < 21; i++)
    {
        q[i]= this.getResources().getIdentifier("q"+i+"_" + number, "string", this.getPackageName());

    }

    int a1_1 = this.getResources().getIdentifier("a1_1_" + number, "string", this.getPackageName());
    int a2_1 = this.getResources().getIdentifier("a2_1_" + number, "string", this.getPackageName());
    int a3_1 = this.getResources().getIdentifier("a3_1_" + number, "string", this.getPackageName());
    int a4_1 = this.getResources().getIdentifier("a4_1_" + number, "string", this.getPackageName());
    int a5_1 = this.getResources().getIdentifier("a5_1_" + number, "string", this.getPackageName());

\\\\

    int a1_20 = this.getResources().getIdentifier("a1_20_" + number, "string", this.getPackageName());
    int a2_20 = this.getResources().getIdentifier("a2_20_" + number, "string", this.getPackageName());
    int a3_20 = this.getResources().getIdentifier("a3_20_" + number, "string", this.getPackageName());
    int a4_20 = this.getResources().getIdentifier("a4_20_" + number, "string", this.getPackageName());
    int a5_20 = this.getResources().getIdentifier("a5_20_" + number, "string", this.getPackageName());
  • 3
    Possible duplicate of [Syntax for creating a two-dimensional array](https://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array) – Roaim Oct 29 '19 at 23:36

1 Answers1

0

You can use the two-dimensional array, here is the code:

int answers[][] = new int[20][5];

for( int i = 0; i < 20; i++) {
    for( int j = 0; j < 5; j++){
         answers[i][j] = this.getResources().getIdentifier("a"+(j+1)+"_"+(i+1)_" + number, "string", this.getPackageName());
    }
}

I am not sure also for the q array since it will start with 0, just revisit it.

Younes Charfaoui
  • 1,059
  • 3
  • 10
  • 20