Reputation: 27
I am new to AndroidStudio. I have managed appending numbers and digits but can't make an addition.
buttonEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText numbers = (EditText) findViewById(R.id.numbers);
int ans = 0;
String str1;
String str2;
if (numbers.toString().contains("+")){
str1 = numbers.toString().substring(0, numbers.toString().indexOf('+'));
str2 = numbers.toString().substring(numbers.toString().indexOf('+') + 1,
numbers.length());
ans = Integer.parseInt(str1) + Integer.parseInt(str2);
numbers.setText(Integer.toString(ans));
}
}
});
This is my code for the addition part (haven't tried other operations yet), when I press Enter, the app does nothing.
Upvotes: 0
Views: 65
Reputation: 3756
You're not getting the text from the EditText
. Add the following method at numbers
before you do the filtering:
numbers.getText()
Upvotes: 2
Reputation: 420
Change this line of code numbers.toString().contains("+")
with this numbers.getText().toString().contains("+")
Upvotes: 1