Entering integers in an arrayist

advertisements

If I have a line of integers in a text file in the following format: [3, 3, 5, 0, 0] how can I go about adding the integers into an arraylist? I have this, but it isn't working:

while (input.hasNextInt())

{
    int tempInt = input.nextInt();
    rtemp.add(tempInt);
}

How do I deal with the commas and the brackets?


You can use ReplaceAll(String regex, String replacement) to remove the brackets and then use Split() function to split the string into an ArrayList using ", " as your delimiter. This will however split the String into smaller strings containing only the numbers so use Integer.parseInt() to convert the string to int.