-1

I have the following code:

View.OnClickListener calcu = new View.OnClickListener() {
    public void onClick(View v) {
        double q;
        String d = "";
        double factor = cpi[to] / cpi [from];
        DecimalFormat decimalFormat = new DecimalFormat("0.##");
        if (eNum.getText().toString().length() <= 0 || bFrom.getText() == "- Select a Year -" || bTo.getText() == "- Select a Year -") {
            if (eNum.getText().toString().length() <= 0) {
                d += "Enter Dollar Amount";
                eNum.setTextColor(Color.RED);
            }
            if (bFrom.getText() == "- Select a Year -") {
                d += "Select a Year";
                bFrom.setTextColor(Color.RED);
            }
            if (bTo.getText() == "- Select a Year -") {
                d += "Select a Year";
                bTo.setTextColor(Color.RED);
            }
        }
        else {
            dollarAmount = factor * Double.parseDouble(eNum.getText().toString());
            String value = Double.toString(dollarAmount);
            if (value.charAt(value.length() - 2) == '.') {
                value += "0";
            }
            displayToast("Dollar: " + value);

            if (cpi[to] != cpi[from]) {
                double f, y;
                if (cpi[to] > cpi[from]) {
                    f = cpi[to] / cpi [from];
                    y = to - from;
                }
                else {
                    f = cpi[from] / cpi[to];
                    y = from - to;
                }
                q = Math.pow(f, 1/y);
                q = (q-1)*100.0;
                q = Math.round(q*100.0)/100.0;
                displayToast("Inflation: " + String.valueOf(decimalFormat.format(q)));
            }
        }
    }
};

displayToast() is a function which displays the Toast message to the user.

eNum is an EditText

bFrom and bTo are Buttons

What I want to happen when the onClick method is activated:

  1. If the eNum is empty, I want to make the text color RED.
  2. If the bFrom and bTo buttons text is - Select a Year -, I want to make the text color RED.
  3. If #1 and #2 is not true than display the Toasts.

Right now when I click, nothing is happening. How do I fix the above code to work correctly?

Si8
  • 9,141
  • 22
  • 109
  • 221

2 Answers2

2

Change this:

bFrom.getText() == "- Select a Year -"

To this:

bFrom.getText().equals("- Select a Year -")

And the same would go for your bTo.getText() == "- Select a Year -") line.

Use .equals() to compare objects (e.g. Strings) and == for primitives

Razgriz
  • 7,179
  • 17
  • 78
  • 150
1

If you are comparing string then you need to use .equals.

Sachin M
  • 151
  • 1
  • 1
  • 12