• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sum of Arraylist

 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For some reason i am getting the following



from this Code
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the error message says, you are trying to parse an empty string into an integer. That won't work, because an empty string cannot meaningfully be parsed into an integer. (What number would you expect that "" means?).
It probably happens in this line:

score.get(i) returns an empty string.

Why: Because you are setting the content of the score list in the onCreate method. At that point, the text boxes are all still empty.

By the way, the String.valueOf(...) is not necessary, since score.get(...) already returns a String. No need to convert a String to a String! You could just as well do this:

(And why do you have so many unnecessary extra parentheses in your code?).
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I had the

in there the last time but still gave me the Same Issue,
Java.lang.NumberFormatExcetion : Invalid int: " "

would i be better off Doing



It stops it from Crashing but nothing is Happening when i click on Calculate Total is still set to zero

 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if Score.get(i) is not getting anything

what do i need to do in order for it to get the elements of the Array ?

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get the content of the edit text fields in the calculate method, instead of in the onCreate method.

onCreate is called when the GUI is initialized (when someone is starting your app). At that moment the user has not yet entered anything into the app. So it doesn't make much sense to get the content of the edit boxes at that moment. You want to do that when the user wants the calculation to be performed.


Are you sure that this is what you want to do? Do you understand what that code means? You want to get the length of each text field in the array? (It's something completely different than what the intent of your original code seems to be).
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to enter Numbers into a Edit Text Field,

Once they are Entered they should be added to the ArrayList Automaically, as Integers
since score.add(T1.getText().toString()); only works on ArrayList that are String
I thought if i got Text to Lenght that might help i also tried

score.add(Integer.parseInt(T1.getText().toString()));

from what you are saying that do not place score.add into Oncreate so my best bet is to place score.add into a TextWatcher ?

if so what is the best way to place an Array into a Text Watcher,

I tired something different which seems to point me at a NullPointer Excption at
EditText T1 = (EditText) findViewById(R.id.editText2); inside editTextArray Class
and also in editArray = new editTextArray(); inside MyActivity Class


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic