Skip to content

Commit 2bcc77b

Browse files
authored
Update MainActivity.java
In this application, the Backspace button is not working which is used to delete the last 1 element. and also update the answer. So I have fixed that problem.
1 parent 997cdda commit 2bcc77b

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

app/src/main/java/com/example/mycalc/MainActivity.java

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,58 @@ public void onClick(View v) {
260260
});
261261

262262
//Backspace button
263-
delete.setOnClickListener(new View.OnClickListener() {
263+
delete.setOnClickListener(new View.OnClickListener() {
264264
@Override
265265
public void onClick(View v) {
266-
267-
int length=getString(answer).length();
268-
//If answer contains only two or less charachter
269-
if (length<3){
270-
clear.performClick();
271-
}
272-
//Or if input endswith +,-,*,/
273-
else if(getString(input).matches(".*[+\\-*/%]$")){
274-
input.setText(getString(input).substring(0,length-1) );
275-
}
276-
//Else delete value from firstVal or secondVal and compute new result and display on answer
277-
else{
278-
input.setText(getString(input).substring(0,length-1) );
279-
if(!secondVal.toString().equals("")){
280-
281-
secondVal.setLength(secondVal.length()-1);
282-
equals.performClick();
266+
String str = input.getText().toString(); //take input value from TextView
267+
//if length of input greater than 1 than its go in if condition
268+
if (str.length() >1 ) {
269+
str = str.substring(0, str.length() - 1); // remove last 1 element from str
270+
input.setText(str); //and set it into input
271+
// Log.i(str + " ","");
272+
if(!secondVal.toString().equals("")){ //secondValue is not null
273+
String value = secondVal.toString();
274+
value = value.substring(0, value.length() - 1); // remove last 1 element from second value
275+
secondVal = new StringBuilder(value);
276+
}else if(!operator.toString().equals("")){ // operator is not null
277+
operator = "";
278+
}else{
279+
String value = firstVal.toString();
280+
value = value.substring(0, value.length() - 1); // remove last 1 element from first value
281+
firstVal = new StringBuilder(value);
283282
}
284-
else{
285283

286-
firstVal.setLength(firstVal.length()-1);
287-
equals.performClick();
288-
}
284+
result=operation(operator,firstVal,secondVal); // perform operation and result
285+
answer.setText("=" + Float.toString(result)); // set result in anwer TextView
286+
}
287+
else if (str.length() <=1 ) {
288+
input.setText("0");
289+
answer.setText("0");
289290
}
291+
// int length=getString(answer).length();
292+
// Log.i(String.valueOf(length)+" ", getString(answer));
293+
// //If answer contains only two or less charachter
294+
// if (length<3){
295+
// clear.performClick();
296+
// }
297+
// //Or if input endswith +,-,*,/
298+
// else if(getString(input).matches(".*[+\\-*/%]$")){
299+
// input.setText(getString(input).substring(0,length-1) );
300+
// }
301+
// //Else delete value from firstVal or secondVal and compute new result and display on answer
302+
// else{
303+
// input.setText(getString(input).substring(0,length-1) );
304+
// if(!secondVal.toString().equals("")){
305+
//
306+
// secondVal.setLength(secondVal.length()-1);
307+
// equals.performClick();
308+
// }
309+
// else{
310+
//
311+
// firstVal.setLength(firstVal.length()-1);
312+
// equals.performClick();
313+
// }
314+
// }
290315

291316
}
292317
});

0 commit comments

Comments
 (0)