Search code examples
javaarraysstringstringtokenizer

splitting string into an array of ints


I have a string s="1,2,3,4,5" . I am using the split() method to split the string then i want to store it into an array of ints.
I tried doing the following but it doesn't work.

int i[]=Integer.parseInt(s.split(","));

I want to know if it is possible to do this without using a loop.

There is something like Array.convertAll in C# but I don't know of a similar thing in java.


Solution

  • If you don't want to use a loop, you can call a method which does the loop for you.

    e.g. (You will need to write the convertToInts method)

    int[] ints = convertToInts(s);