Reputation: 9
I am trying to code a simple calculator. My code showed up no errors and also uploaded successfully, but when I try to enter numbers the display always remains zero. The code is like this: Coding for android 2.3.3
public class MainActivity extends Activity {
TextView res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = (TextView)findViewById(R.id.textView1);
res.setText("0");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
static boolean empty = true;
public void num_clicked(View sender)
{
Button bt = (Button)sender;
if(res.getText().length() > 8) return;
if(empty)
{
if(bt.getText().toString().equals("0")) return;
res.setText(bt.getText());
empty = false;
}
else
{
res.append(bt.getText());
}
}
static int acc = 0;
static short op;
public void op_clicked(View sender)
{
Button bt = (Button)sender;
switch(op)
{
case 0:
acc+=Integer.parseInt(res.getText().toString());
break;
case 1:
acc-=Integer.parseInt(res.getText().toString());
break;
case 2:
acc*=Integer.parseInt(res.getText().toString());
break;
case 3:
acc/=Integer.parseInt(res.getText().toString());
break;
}
res.setText(Integer.toString(acc));
if(bt.getText().toString().equals("+")) op = 0;
if(bt.getText().toString().equals("-")) op = 1;
if(bt.getText().toString().equals("*")) op = 2;
if(bt.getText().toString().equals("/")) op = 3;
empty = true;
}
}
Upvotes: 0
Views: 190
Reputation: 13483
First of all, you have some incorrect code at the bottom:
if(bt.getText().toString().equals("+")) op = 0;
if(bt.getText().toString().equals("+")) op = 1;
if(bt.getText().toString().equals("+")) op = 2;
if(bt.getText().toString().equals("+")) op = 3;
empty = true;
because you test for "+"
every time so op
will always be 3
when a plus sign is entered and 0
otherwise.
Secondly, you are resetting empty
to be true
at the end, no matter what. So, this will make the display 0
:
if(empty)
{
if(bt.getText().toString().equals("0")) return;
res.setText(bt.getText());
empty = false;
}
I don't know how you've set up your XML layout so I don't know what if the button listeners are properly implemented. This is a start of your problems.
The variable acc
is only ever touched in your switch statement, so since it is 0
by default, /=
and *=
will still leave it at 0
. That's a problem.
This code is probably also wrong:
switch(op)
{
case 0:
acc+=Integer.parseInt(res.getText().toString());
break;
case 1:
acc-=Integer.parseInt(res.getText().toString());
break;
case 2:
acc*=Integer.parseInt(res.getText().toString());
break;
case 3:
acc/=Integer.parseInt(res.getText().toString());
break;
}
res.setText(Integer.toString(acc));
if(bt.getText().toString().equals("+")) op = 0;
if(bt.getText().toString().equals("-")) op = 1;
if(bt.getText().toString().equals("*")) op = 2;
if(bt.getText().toString().equals("/")) op = 3;
because your are assigning a value to op
after you have already used a switch statement with op
. Put the if statements before the switch statement.
Upvotes: 1
Reputation: 2702
It seems that you didn't use setOnClickListener
to set the OnClickListener of Buttons. Or write the android:onClick
in your layout file.
Upvotes: 0