Original question is as follows in the text box it should display 1 + 2 and upon clicking equal it should display the sum. With three values there are many combination 1+1,2+3+3, 1+3+3 .... but it is not working please help? the codes are not complete i'm just testing it with just three values and the addition operation

myCodes:

MainActivity
public class MainActivity extends Activity {

    Button btn1;
    Button btn2;
    Button btn3;
    Button btnAdd;
    Button btnSub;
    String btn_val1;
    String btn_val2;    
    String btn_val3;
    String btn_valAdd;
    String btn_valSub;

    EditText    edit_text;

    TextView textview;

    int res;

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

    public void btn1Click(View view)
    {
        btn1 = (Button) findViewById(R.id.btn1);
        btn_val1  =   btn1.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview .append(btn_val1);
    }

    public void btn2Click(View view)
    {
         btn2 = (Button) findViewById(R.id.btn2);
        btn_val2  =   btn2.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview .append(btn_val2);
    }

    public void btn3Click(View view)
    {
         btn3 = (Button) findViewById(R.id.btn3);
        btn_val3  =   btn3.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview.append(btn_val3);
    }

    public void btnAddClick(View view)
    {
         btnAdd = (Button) findViewById(R.id.btnAdd);
        //res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
        btn_valAdd  =   btnAdd.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview.append(btn_valAdd);

    }

    public void btnSubClick(View view)
    {
        btnSub = (Button) findViewById(R.id.btnSub);
        //res = Integer.parseInt(btn_val1) - Integer.parseInt(btn_val2) - Integer.parseInt(btn_val3);
        btn_valSub = btnSub.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview.append(btn_valSub);

    }

    public void btnEq(View view)
    {

        textview  = (TextView) findViewById(R.id.textView2);
        if(btnAdd != null)
        {
            if(btn_val1 != null && btn_val2 != null )
            {

                res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2);
                textview.setText(String.valueOf(res));
            }

            else if(btn_val1 != null && btn_val3 != null)
            {
                res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val3);
                textview.setText(String.valueOf(res));
            }

            else if(btn_val2 != null && btn_val3 != null)
            {
                res = Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
                textview.setText(String.valueOf(res));
            }

            else if(btn_val1 != null && btn_val2 != null && btn_val3 != null)
            {
            res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
            textview.setText(String.valueOf(res));
            }
        }

        else if(btnSub != null)
        {
            res = Integer.parseInt(btn_val1) - Integer.parseInt(btn_val2) - Integer.parseInt(btn_val3);
            textview.setText(String.valueOf(res));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}



ActivityMain
<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        tools:context=".MainActivity" />



    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="36dp"
        android:text="1" 
        android:onClick="btn1Click"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn1"
        android:layout_alignBottom="@+id/btn1"
        android:layout_toRightOf="@+id/btn1"
        android:text="2"
        android:onClick="btn2Click" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn2"
        android:layout_alignBottom="@+id/btn2"
        android:layout_toRightOf="@+id/btn2"
        android:text="3" 
        android:onClick="btn3Click"/>

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btn1"
        android:layout_marginTop="15dp"
        android:text="4" />

    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn4"
        android:layout_alignBottom="@+id/btn4"
        android:layout_toRightOf="@+id/btn4"
        android:text="5" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn3"
        android:layout_alignParentRight="true"
        android:text="+" 
        android:onClick="btnAddClick"/>

    <Button
        android:id="@+id/btnSub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn5"
        android:layout_alignParentRight="true"
        android:text="-" 
        android:onClick="btnSubClick"/>

    <Button
        android:id="@+id/btnDiv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:text="/" />

    <Button
        android:id="@+id/btnMul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnDiv"
        android:layout_marginTop="14dp"
        android:text="*" />



    <Button
        android:id="@+id/btnEqual"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnMul"
        android:layout_marginTop="15dp"
        android:text="=" 
        android:onClick="btnEq"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText1"
        android:gravity="right"
        android:textAppearance="?android:attr/textAppearanceLarge" />

Recommended Answers

All 5 Replies

Indent your code first. It'll at least make it readable.

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button btn1;
    Button btn2;
    Button btn3;
    Button btnAdd;
    Button btnSub;
    String btn_val1;
    String btn_val2;
    String btn_val3;
    String btn_valAdd;
    String btn_valSub;

    EditText    edit_text;

    TextView textview;

    int res;

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

    public void btn1Click(View view)
    {
        btn1 = (Button) findViewById(R.id.btn1);
        btn_val1  =   btn1.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview .append(btn_val1);
    }

    public void btn2Click(View view)
    {
        btn2 = (Button) findViewById(R.id.btn2);
        btn_val2  =   btn2.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview .append(btn_val2);
    }

    public void btn3Click(View view)
    {
        btn3 = (Button) findViewById(R.id.btn3);
        btn_val3  =   btn3.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview.append(btn_val3);
    }

    public void btnAddClick(View view)
    {
        btnAdd = (Button) findViewById(R.id.btnAdd);
        //res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
        btn_valAdd  =   btnAdd.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview.append(btn_valAdd);

    }

    public void btnSubClick(View view)
    {
        btnSub = (Button) findViewById(R.id.btnSub);
        //res = Integer.parseInt(btn_val1) - Integer.parseInt(btn_val2) - Integer.parseInt(btn_val3);
        btn_valSub = btnSub.getText().toString();
        textview  = (TextView) findViewById(R.id.textView2);
        textview.append(btn_valSub);

    }

    public void btnEq(View view)
    {

        textview  = (TextView) findViewById(R.id.textView2);
        if(btnAdd != null)
        {
            if(btn_val1 != null && btn_val2 != null )
            {

                res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2);
                textview.setText(String.valueOf(res));
            }

            else if(btn_val1 != null && btn_val3 != null)
            {
                res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val3);
                textview.setText(String.valueOf(res));
            }

            else if(btn_val2 != null && btn_val3 != null)
            {
                res = Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
                textview.setText(String.valueOf(res));
            }

            else if(btn_val1 != null && btn_val2 != null && btn_val3 != null)
            {
                res = Integer.parseInt(btn_val1) + Integer.parseInt(btn_val2) + Integer.parseInt(btn_val3);
                textview.setText(String.valueOf(res));
            }
        }

        else if(btnSub != null)
        {
            res = Integer.parseInt(btn_val1) - Integer.parseInt(btn_val2) - Integer.parseInt(btn_val3);
            textview.setText(String.valueOf(res));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

what is the button you are clicking, what is your expected result, and what is your actual result?

if i click 1 + 2 then i click = it gives me 3 but if i continue to add like + 2 and i click = it crashes. Also if i do 3+3+3 and i click = the program crashes. So i want my program to be able to perform addition correctly whatever combination i use either 2+3+3+3 or 1+1+1+2 and when i press = the answer should be correct i'm stuck on this part. Hope it helps to understand my problem

quite a lot wrong with your code, besides the fact that you are using horrible names and types.
don't load "3" in a String, if you know that it's supposed to be a numerical value you want to perform calculations on, use a numerical type, like Integer.
also, give your variables meaningful names. btnAdd is not the name you should give to a numerical value, but to a button.
following mistake (quite a big one)

if(btnAdd != null)
        {
            if(btn_val1 != null && btn_val2 != null )
            {

since you never re-set the values of these, if you click another button in the next calculation, it'll still take these two.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.