Reputation: 23
I'm making a calculator for Android Wear, everything is going well but I have a problem with decimal numbers. When I do a simple operation like 95/2 the result is 47 instead of 47.5 . Also when I write a decimal number such as 5.5 or 9.4 and I click on +, -, *, or /, the emulator says "Unfortunately application has stopped". This is my Activity:
public class MyActivity extends Activity implements View.OnClickListener {
Button button0, button1, button2, button3, button4, button5, button6 , button7, button8, button9,
buttonClear, buttonDelete, buttonEqual, buttonMin, buttonPlus, buttonX,
buttonSlash, buttonDot;
EditText editText;
int op1;
int op2;
String optr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rect_activity_my);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
editText = (EditText) findViewById(R.id.editText);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonEqual = (Button) findViewById(R.id.buttonEqual);
buttonSlash = (Button) findViewById(R.id.buttonSlash);
buttonDelete = (Button) findViewById(R.id.buttonDelete);
buttonX = (Button) findViewById(R.id.buttonX);
buttonMin = (Button) findViewById(R.id.buttonMin);
buttonPlus = (Button) findViewById(R.id.buttonPlus);
buttonDot = (Button) findViewById(R.id.buttonDot);
try{
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonClear.setOnClickListener(this);
buttonDelete.setOnClickListener(this);
buttonEqual.setOnClickListener(this);
buttonMin.setOnClickListener(this);
buttonPlus.setOnClickListener(this);
buttonX.setOnClickListener(this);
buttonSlash.setOnClickListener(this);
buttonDot.setOnClickListener(this);
}
catch(Exception e){
}
}
public void operation(){
if(optr.equals("+")){
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 + op2;
editText.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("-")){
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 - op2;
editText.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("*")){
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 * op2;
editText.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("/")){
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 / op2;
editText.setText("Result : " + Integer.toString(op1));
}
}
@Override
public void onClick(View arg0) {
Editable str = editText.getText();
switch(arg0.getId()){
case R.id.button1:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button1.getText());
editText.setText(str);
break;
case R.id.button2:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button2.getText());
editText.setText(str);
break;
case R.id.button3:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button3.getText());
editText.setText(str);
break;
case R.id.button4:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button4.getText());
editText.setText(str);
break;
case R.id.button5:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button5.getText());
editText.setText(str);
break;
case R.id.button6:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button6.getText());
editText.setText(str);
break;
case R.id.button7:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button7.getText());
editText.setText(str);
break;
case R.id.button8:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button8.getText());
editText.setText(str);
break;
case R.id.button9:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button9.getText());
editText.setText(str);
break;
case R.id.button0:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button0.getText());
editText.setText(str);
break;
case R.id.buttonClear:
op1 = 0;
op2 = 0;
editText.setText("");
break;
case R.id.buttonDot:
if (op1 == 0){
op1 = Integer.parseInt(editText.getText().toString());
editText.setText(op1 + ".");
} else if (op2 == 0){
op2 = Integer.parseInt(editText.getText().toString());
editText.setText(op2 + ".");
}
break;
case R.id.buttonDelete:
editText.setText(str);
if (str.length() > 1){
str = (Editable) str.subSequence(0, str.length() - 1);
editText.setText(str);
}else if (str.length() <= 1){
editText.setText("");
}
break;
case R.id.buttonPlus:
optr = "+";
if(op1 == 0){
op1 = Integer.parseInt(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 + op2;
editText.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.buttonMin:
optr = "-";
if(op1 == 0){
op1 = Integer.parseInt(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 - op2;
editText.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.buttonX:
optr = "*";
if(op1 == 0){
op1 = Integer.parseInt(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 * op2;
editText.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.buttonSlash:
optr = "/";
if(op1 == 0){
op1 = Integer.parseInt(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Integer.parseInt(editText.getText().toString());
editText.setText("");
op1 = op1 / op2;
editText.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.buttonEqual:
if(!optr.equals(null)){
if(op2 != 0){
if(optr.equals("+")){
editText.setText("");
/*op1 = op1 + op2;*/
editText.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("-")){
editText.setText("");/*
op1 = op1 - op2;*/
editText.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("*")){
editText.setText("");/*
op1 = op1 * op2;*/
editText.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("/")){
editText.setText("");/*
op1 = op1 / op2;*/
editText.setText("Result : " + Integer.toString(op1));
}
}
else{
operation();
}
}
break;
}
}
}
How can I solve this issue?
Upvotes: 2
Views: 116
Reputation: 1786
In Java, Integer division automatically truncates any decimal values.
One way to fix this is to modify the division statment op1 = op1 / op2;
to something similar to the following:
op1 = op1 / (double) op2
The statement Integer.parseInt()
will have problems converting a decimal number since integers cannot have decimals. That's probably why you have the error "Unfortunately application has stopped" when you try to input numbers with decimals.
To account for decimals, you should try a boolean or a float variable type to store your numbers.
Upvotes: 1
Reputation: 994
You are using int
, which always returns whole numbers. Instead, you want to use a double
, which can return a decimal. So, in your variable declarations:
double op1;
double op2;
double result;
In your operations:
public void operation(){
if(optr.equals("+")){
result = op1 + op2;
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("-")){
result = op1 - op2;
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("*")){
result = op1 * op2;
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("/")){
result = op1 / op2;
editText.setText("Result : " + Double.toString(result));
}
You will have to refactor some of your code and replace all instances of Integer
with Double
.
Upvotes: 0
Reputation: 2720
You are using integers instead of floats. Integer numbers cannot have decimals.
Change lines like int op1; to float op1;
and
op2 = Integer.parseFloat(editText.getText().toString()); to op2 = Float.parseFloat(editText.getText().toString());
Upvotes: 0