I have a program that I am trying to take a set of numbers from a string separated by commas and place them into an ArrayList; however, I’m not quite sure how to do it. So far what I have done is turn the String into an array of chars and then convert the chars into ints by using:
Character.digit(temp[i], 10)
This example is in a for loop iterating over a string. Let’s say in this case "1,2,3,4". taking the first element of the new char array and converting it to a int.
My issue is,
- A: there has to be a better way of doing this.
- B: what happens if you get a 2 or three digit number instead, e.g,
"34,2,3,65,125". these will be stored as separate elements of the array when i need it to be one element. - C: what happens if the number is a negative one, and what if that negative number is 2 or three digits long? E.g.,
"-123,45,3,4,-6".
Remember that this is mean to be for any String argument.
There are lots of conditions here and I’m not sure how to solve them.
you could just do: