-1

I am a beginner working on an android application in which i am stuck at comparing the value taken from previous activity and a variable initialized with the same value as the previous one.

This is the mainactivity code

     String string="value";
    reference.setText(string);//i am setting the value manually to Edittext       
    Intent i = new Intent(this,next.class);
    String getrec=reference.getText().toString();
    Bundle bundle = new Bundle();
    bundle.putString("VALUER", getrec);
    System.out.print(getrec);
    i.putExtras(bundle);
    startActivity(i);
    finish();

This the next.class code

           int i;
            Bundle bundle = getIntent().getExtras();
             String venName = bundle.getString("VALUER");
                if (venName=="value") {
                    i=1;
                    ...
                  }

And this if condition is not getting executed. Please help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
verma
  • 1
  • Why don't you try printing out what the value is with the `Logger`? `Log.e("Value is: ", venName);` – MacLean Sochor Jul 11 '17 at 17:27
  • In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object references, not the content. – Ali Jul 11 '17 at 17:29

2 Answers2

0

You can not compare strings using ==. Use .equals() instead and it will fix the bug.

int i;
Bundle bundle = getIntent().getExtras();
String venName = bundle.getString("VALUER");
if (venName.equals("value")) {
   i=1;
   ...
}
Amit Kumar
  • 1,428
  • 2
  • 12
  • 20
0

In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object references, not the content. == is for testing whether two strings are the same object, whereas .equals() tests whether two strings have the same value.

 int i;
        Bundle bundle = getIntent().getExtras();
         String venName = bundle.getString("VALUER");
            if (venName.equals("value") {
                i=1;
                ...
              }
Ali
  • 839
  • 11
  • 21