The error is in the following line:
numberArray.add(Integer.parseInt(txtNumber.getText()));
Answering your code would be easier if you have added the stacktrace which is the error code you receive. It looks similar to the following:
Exception in thread "main" java.lang.NumberFormatException: For input string: "6 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
The crucial part of the stacktrace in this case it the first line. It says what input caused the problem. The rest of the stacktrace says exactly what caused the Exception to be thrown.
If you update the question with the stacktrace, I'll fully answer your question. Now, let me guess what might help.
Your problem might be caused by three situations:
- You're trying to parse a
null
,
- Your
String
is lead or followed by white characters which you don't see or is empty,
- The
String
isn't parsable at all.
I believe it's the second option, because otherwise you would notice the problem from the stacktrace.
The solution might be:
numberArray.add(Integer.parseInt(
(txtNumber.getText().trim().equals("") ?
"0"
:
txtNumber.getText().trim()
));
if it still throws an Exception please give me a comment and update the question with a stacktrace.