0

I am trying to make an if statement to change the value of 3 variables. The problem is that i don't get any value even if the condition is true:

here I get the string that I want to check:

    final String[] pos_categ ;
    pos_categ = intent1.getExtras().getStringArray("categ");

Here I implement the variables that i want to change:

    String[] title = null;
    String[] eng = null;
    final String[] dan = null;

Here is the if statement:

    if (pos_categ[0]==lessons_titles[0]){
        title[0] = lessons_titles[position[0]];
        eng[0] = eng_version[position[0]];
        dan[0] = dan_version[position[0]].toLowerCase();
    }
    if (pos_categ[0]==lessons_titles2[0]){
        title[0] = lessons_titles2[position[0]];
        eng[0] = eng_version2[position[0]];
        dan[0] = dan_version2[position[0]].toLowerCase();
    }
    if (pos_categ[0]==lessons_titles3[0]){
        title[0] = lessons_titles3[position[0]];
        eng[0] = eng_version3[position[0]];
        dan[0] = dan_version3[position[0]].toLowerCase();
    }
    if (pos_categ[0]==lessons_titles4[0]){
        title[0] = lessons_titles4[position[0]];
        eng[0] = eng_version4[position[0]];
        dan[0] = dan_version4[position[0]].toLowerCase();
    }
    if (pos_categ[0]==lessons_titles5[0]){
        title[0] = lessons_titles5[position[0]];
        eng[0] = eng_version5[position[0]];
        dan[0] = dan_version5[position[0]].toLowerCase();
    }
    if (pos_categ[0]==lessons_titles6[0]){
        title[0] = lessons_titles6[position[0]];
        eng[0] = eng_version6[position[0]];
        dan[0] = dan_version6[position[0]].toLowerCase();
    }

    title_view.setText(title[0]);
    eng_view.setText(eng[0]);
    dan_view.setText(dan[0]);

Can someone tell me where I've made a mistake ?

Sudhir Mishra
  • 578
  • 2
  • 16
Victor Mihaita
  • 106
  • 1
  • 2
  • 10

3 Answers3

4

In Java, use string.equals() to check string equality by content:

if (pos_categ[0].equals(lessons_titles[0])){
  ....
}
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
0

use .equals for string dude like String1.equals(string2) when ever we use harcode string like "string" then == and .equals both are same but whenever we use string object then always use .equals

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0

Use The Code Below:

if (pos_categ[0].equals(lessons_titles[0])){
            title[0] = lessons_titles[position[0]];
            eng[0] = eng_version[position[0]];
            dan[0] = dan_version[position[0]].toLowerCase();
        }
        if (pos_categ[0].equals(lessons_titles2[0])){
            title[0] = lessons_titles2[position[0]];
            eng[0] = eng_version2[position[0]];
            dan[0] = dan_version2[position[0]].toLowerCase();
        }
        if (pos_categ[0].equals(lessons_titles3[0])){
            title[0] = lessons_titles3[position[0]];
            eng[0] = eng_version3[position[0]];
            dan[0] = dan_version3[position[0]].toLowerCase();
        }
        if (pos_categ[0].equals(lessons_titles4[0])){
            title[0] = lessons_titles4[position[0]];
            eng[0] = eng_version4[position[0]];
            dan[0] = dan_version4[position[0]].toLowerCase();
        }
        if (pos_categ[0].equals(lessons_titles5[0])){
            title[0] = lessons_titles5[position[0]];
            eng[0] = eng_version5[position[0]];
            dan[0] = dan_version5[position[0]].toLowerCase();
        }
        if (pos_categ[0].equals(lessons_titles6[0])){
            title[0] = lessons_titles6[position[0]];
            eng[0] = eng_version6[position[0]];
            dan[0] = dan_version6[position[0]].toLowerCase();
        }

        title_view.setText(title[0]);
        eng_view.setText(eng[0]);
        dan_view.setText(dan[0]);
Jigar
  • 791
  • 11
  • 21