Shubham Sharma Shubham Sharma - 4 months ago 10
Android Question

If statement logic help needed

I am creating a calculator using android studio.
I don't want to append operators in the edittext if it is empty and i have implemented the same using setOnclicklistner(); but it doesn't seem to work the intended way. The operators get appended even if the edittext is empty(only with +,- and % as i have used android:xml for multiply).I am curious to know why it is happening. Here are my files

MyActivity.java

package com.example.meenakshi.calculator;


import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;
import bsh.EvalError;
import bsh.Interpreter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

EditText text;
Button btn;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.result);

findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
findViewById(R.id.button10).setOnClickListener(this);
//findViewById(R.id.button11).setOnClickListener(this);
findViewById(R.id.button12).setOnClickListener(this);
findViewById(R.id.button13).setOnClickListener(this);
findViewById(R.id.button14).setOnClickListener(this);
//findViewById(R.id.button15).setOnClickListener(this);
findViewById(R.id.button16).setOnClickListener(this);
}


public void onClick(View v){
btn = (Button)v;
if(text.getText().toString().trim().length() == 0){
if(btn.getText().toString() != "+" && btn.getText().toString()!= "-" &&btn.getText().toString()!= "%" /*&& btn.getText().toString()!= "*"*/ )
text.append(btn.getText());
}else{
text.append(btn.getText());
}
}
public void mul(View view){
btn = (Button)view;
if(text.getText().toString().trim().length()!= 0)
text.append(btn.getText());
}
public void calculate(View view){

Interpreter in = new Interpreter();
if(text.getText().toString().length()!=0) {
try {
text.setText("="+in.eval(text.getText().toString()).toString());
} catch (EvalError e) {
text.setText(e.getErrorText());
}
}
}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.meenakshi.calculator.MainActivity">

<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:id="@+id/button"
android:layout_row="0"
android:layout_column="0"
android:textColor="@color/abc_search_url_text_pressed"
android:nestedScrollingEnabled="false" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button16"
android:layout_row="0"
android:layout_column="3"
android:text="@string/buttond_text" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button4_text"
android:id="@+id/button4"
android:layout_row="1"
android:layout_column="0" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "@string/button2_text"
android:id="@+id/button2"
android:layout_row="0"
android:layout_column="1"
android:onClick="press2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button3_text"
android:id="@+id/button3"
android:layout_row="0"
android:layout_column="2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonmul_text"
android:id="@+id/button15"
android:layout_row="1"
android:layout_column="3"
android:onClick="mul" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button7_text"
android:id="@+id/button7"
android:layout_row="2"
android:layout_column="0" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button5_text"
android:id="@+id/button5"
android:layout_row="1"
android:layout_column="1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button6_text"
android:id="@+id/button6"
android:layout_row="1"
android:layout_column="2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button8_text"
android:id="@+id/button8"
android:layout_row="2"
android:layout_column="1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonm_text"
android:id="@+id/button14"
android:layout_row="2"
android:layout_column="3" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button._text"
android:id="@+id/button12"
android:layout_row="3"
android:layout_column="0" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button0_text"
android:id="@+id/button10"
android:layout_row="3"
android:layout_column="1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button9_text"
android:id="@+id/button9"
android:layout_row="2"
android:layout_column="2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttone_text"
android:id="@+id/button11"
android:layout_row="3"
android:layout_column="2"
android:focusable="false"
android:onClick="calculate" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonp_text"
android:id="@+id/button13"
android:layout_row="3"
android:layout_column="3" />
</GridLayout>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:inputType="text" />

</RelativeLayout>

Answer

Always compare Strings like this.

btn.getText().toString().equalsIgnoreCase("+");
Comments