-5

In my applicatin I want to change the value of a variable dynamically , Based on string . Here is what I have tried

public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btCalculate:

                int column = Integer.parseInt((String) spColumn
                        .getSelectedItem());
                int inch = Integer.parseInt((String) spInch.getSelectedItem());
                int colum_inch = column*inch ;
                int MULTIUPLIER ;

                if (page_number=="FirstPage(Color)Minimum12Maximum80ColumnInch-14500PerColumn/Inch")

                {
                        MULTIUPLIER=20;
                } 
                else if (page_number=="3rd Page Color: 9000") {
                    MULTIUPLIER=9000;

                }
                else {
                        MULTIUPLIER=10;
                }
                if (colum_inch<4) {
                    tvAmount.setText(page_number);

                } else {
                    tvAmount.setText((MULTIUPLIER *colum_inch) + "");
                }

                break;
        }
    }

Here the variable page_number is a string which stores the text of clicked button . In my previous activity I have two button , one button text is "FirstPage(Color)Minimum12Maximum80ColumnInch-14500PerColumn/Inch" another "3rd Page Color: 9000" . But the problem is in my out the MULTIPLIER is always set to "10" . No matter which button I have clicked . How can I resolve this . How can I change the value of that variable (MULTIPLIER) based the text of clicked button .

Mithun Sarker
  • 25
  • 1
  • 5

2 Answers2

4

You cannot compare string using the == equality operator. == tests whether 2 objects are the same object.

Use .equals() instead.

Because of string interning, this statement may or may not be true but you have no control over that.

page_number=="FirstPage(Color)Minimum12Maximum80ColumnInch-14500PerColumn/Inch"

This will always work.

page_number.equals("FirstPage(Color)Minimum12Maximum80ColumnInch-14500PerColumn/Inch")
Simon
  • 14,407
  • 8
  • 46
  • 61
2

use equals and not == this is the way java compares strings

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Ofer
  • 4,879
  • 6
  • 20
  • 26