Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using StringBuilder in loop  RSS feed

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to loop through a string type field from a recordset that I am extracting where if the length of the string value is less than 6 then pad the value with a "0" (zero). Sometimes the value may a length of 2, 3, 4 or 5 and I am needing the amount of zeros to bring the length of this value to 6.

Right now I am running into a value that has a length of 5 and I am getting a IndexOutOfBounds of 0 error.

Here is the code I attempted and could really use any help or direction (not looking to be spoon fed but the deeper I get in the more confused I am getting) so thanks in advance for any assistance:

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line creates an array of StringBuilder references, but doesn't actually create any StringBuilder objects, so i would have expected this lineto throw a NullPointerException.

Anyway, a better way to pad a string with zeros is something like
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne,

Thanks so much for your time in replying. This is what I did based on help from a colleague and hopefully it will help some other newbie on how to handle a StringBuilder in a for loop:



Regards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!