samedi 19 septembre 2015

if-else not working in for loop for string in android

I'm trying to make a calculator. but condition where I am assigning value to num2 is not working. unable to figure out what is wrong with the code.

MainActivity.java

    package rihan.calc3;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnLongClickListener;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnClickListener {

        Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnadd, btnsub, btnmul, btndiv, btndot, btneq,
        btnclr;
        RelativeLayout rel;
        TextView txt;

        int dot = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btn1 = (Button) findViewById(R.id.btn1);
            btn2 = (Button) findViewById(R.id.btn2);
            btn3 = (Button) findViewById(R.id.btn3);
            btn4 = (Button) findViewById(R.id.btn4);
            btn5 = (Button) findViewById(R.id.btn5);
            btn6 = (Button) findViewById(R.id.btn6);
            btn7 = (Button) findViewById(R.id.btn7);
            btn8 = (Button) findViewById(R.id.btn8);
            btn9 = (Button) findViewById(R.id.btn9);
            btn0 = (Button) findViewById(R.id.btn0);
            btnadd = (Button) findViewById(R.id.btnadd);
            btnsub = (Button) findViewById(R.id.btnsub);
            btnmul = (Button) findViewById(R.id.btnmul);
            btndiv = (Button) findViewById(R.id.btndiv);
            btndot = (Button) findViewById(R.id.btndot);
            btneq = (Button) findViewById(R.id.btneq);
            btnclr = (Button) findViewById(R.id.btnclr);
            rel = (RelativeLayout) findViewById(R.id.rel);
            txt = (TextView) findViewById(R.id.txt);

            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
            btn6.setOnClickListener(this);
            btn7.setOnClickListener(this);
            btn8.setOnClickListener(this);
            btn9.setOnClickListener(this);
            btn0.setOnClickListener(this);
            btnadd.setOnClickListener(this);
            btnsub.setOnClickListener(this);
            btnmul.setOnClickListener(this);
            btndiv.setOnClickListener(this);
            btndot.setOnClickListener(this);
            btneq.setOnClickListener(this);
            btnclr.setOnClickListener(this);

            btnclr.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub

                    return false;
                }
            });
        }

        @SuppressWarnings("null")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
            case R.id.btn1:
                txt.append("1");
                break;

            case R.id.btn2:
                txt.append("2");
                break;

            case R.id.btn3:
                txt.append("3");
                break;

            case R.id.btn4:
                txt.append("4");
                break;

            case R.id.btn5:
                txt.append("5");
                break;

            case R.id.btn6:
                txt.append("6");
                break;

            case R.id.btn7:
                txt.append("7");
                break;

            case R.id.btn8:
                txt.append("8");
                break;

            case R.id.btn9:
                txt.append("9");
                break;

            case R.id.btn0:
                txt.append("0");
                break;

            case R.id.btnadd:
                txt.append(" + ");
                dot = 0;
                break;

            case R.id.btnsub:
                txt.append(" - ");
                dot = 0;
                break;

            case R.id.btndiv:
                txt.append(" / ");
                dot = 0;
                break;

            case R.id.btnmul:
                txt.append(" x ");
                dot = 0;
                break;

            case R.id.btneq:
                String[] txt_arr = txt.getText().toString().split(" ");
                String symbol = "i";

                boolean symbol_flag = false;
                int array_index = 0;

                float num1 = 0, num2 = 0, result = 0;

                for (array_index = 0; array_index < txt_arr.length - 1; array_index++) {
                    if (txt_arr[array_index].contains("+")) {
                        symbol = txt_arr[array_index];
                        symbol_flag = true;
                    }

                    else {
                        if (symbol_flag == true) {
                            num2 = Float.parseFloat(txt_arr[array_index]);
                            result = num1 + num2;
                            num1 = result;
                        }

                        else {
                            num1 = Float.parseFloat(txt_arr[array_index]);
                        }
                    }
                }

                Toast.makeText(MainActivity.this,
                "result : " + result + ". num1 : " + num1 + ". Symbol is : " + symbol + ". Symbol flag is : "
                        + symbol_flag +". num2 : " + num2 + ". length is : " + txt_arr.length,
                Toast.LENGTH_SHORT).show();
                // array_index=0;
                break;

            case R.id.btnclr:

                break;

            case R.id.btndot:
                break;

            default:
                break;
            }

        }

    }

no operation is being performed on num2. IF condition is not getting processed. however its else condition is working fine

                    **else {
                        if (symbol_flag == true) {
                            num2 = Float.parseFloat(txt_arr[array_index]);
                            result = num1 + num2;
                            num1 = result;
                        }**

                        else {
                            num1 = Float.parseFloat(txt_arr[array_index]);
                        }
                    }

activity_main.xml

    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
        android:id="@+id/rel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/background_dark" >

        <TextView
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:maxLength="25"
            android:text="0"
            android:textAlignment="textEnd"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:background="@android:color/background_dark"
            android:text="1"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button        
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/btn1"
            android:background="@android:color/background_dark"
            android:text="2"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/btn2"
            android:background="@android:color/background_dark"
            android:text="3"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnadd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/btn3"
            android:background="@android:color/background_dark"
            android:text="+"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn1"
            android:background="@android:color/background_dark"
            android:text="4"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn2"
            android:layout_toRightOf="@id/btn4"
            android:background="@android:color/background_dark"
            android:text="5"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn3"
            android:layout_toRightOf="@id/btn5"
            android:background="@android:color/background_dark"
            android:text="6"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnsub"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnadd"
            android:layout_toRightOf="@id/btn6"
            android:background="@android:color/background_dark"
            android:text="-"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn4"
            android:background="@android:color/background_dark"
            android:text="7"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn5"
            android:layout_toRightOf="@id/btn7"
            android:background="@android:color/background_dark"
            android:text="8"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn6"
            android:layout_toRightOf="@id/btn8"
            android:background="@android:color/background_dark"
            android:text="9"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnmul"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnsub"
            android:layout_toRightOf="@id/btn9"
            android:background="@android:color/background_dark"
            android:text="x"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btndot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn7"
            android:background="@android:color/background_dark"
            android:text="."
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btn0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn8"
            android:layout_toRightOf="@id/btndot"
            android:background="@android:color/background_dark"
            android:text="0"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btnclr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btn9"
            android:layout_toRightOf="@id/btn0"
            android:background="@android:color/background_dark"
            android:text="C"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btndiv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnmul"
            android:layout_toRightOf="@id/btnclr"
            android:background="@android:color/background_dark"
            android:text="/"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <Button
            android:id="@+id/btneq"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btndot"
            android:background="@android:color/background_dark"
            android:text="="
            android:textColor="@android:color/white"
            android:textSize="50sp" />

        <TextView
            android:id="@+id/txt1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/btneq"
            android:maxLength="25"
            android:textColor="@android:color/white"
            android:textSize="50sp" />

    </RelativeLayout>

txt1 is of no use. please ignore that.

KINDLY TELL ME WHAT IS WRONG WITH THAT IF CONDITION WITHIN DOUBLE ASTERISKS

Aucun commentaire:

Enregistrer un commentaire