I need a little help with a calculator program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

I need a little help with a calculator program

I tried to do a calculator program. I added the buttons and a textfield to show the user's income and output as well. My problem starts when somebody write for example "5+2". It is a String value what a tried to transform to int value to calculate the sum (Integer.parseInt(textfield.getText()). After that I would like to transform the result back String value to add this value to the textfield (String.instanceof(Integer.parseInt(textfield.getText().But I always get a NumberformatException. Anybody can help?

30th Dec 2017, 11:14 AM
István Vámosi
István Vámosi - avatar
4 Answers
+ 9
try this , String numberone = edittext.getText().toString(); String numbertwo = edittext1.getText().toString(); int addition = Integer.parseInt(numberone) + Integer.parseint(numbertwo); TextView.setText(String.valueof(addition));
31st Dec 2017, 5:51 AM
Shane Umayanga
Shane Umayanga - avatar
+ 6
because you can't parse 5+2 as one int because 5 is one integer, + is operator and 2 is another integer. You could try to threat that string as array of characters and parse his first and second character.
31st Dec 2017, 1:33 AM
Vukan
Vukan - avatar
+ 6
you're welcome 😁
31st Dec 2017, 12:07 PM
Shane Umayanga
Shane Umayanga - avatar
+ 3
Thank you to all the help. I will try your suggestions.
31st Dec 2017, 10:56 AM
István Vámosi
István Vámosi - avatar