Isam H. Abuaqlain :
I have a method that accepts any number of INTEGER parameters: pages(int,int...)
This method is to select some pages of PDF file.
Book Pages stored as a string type like this:
String pages = "1,2,3,6,9";
I want to make this string as the parameter of the method to look like:
pages(1,2,3,6,9);
BackSlash :
This can be easily done with streams:
Stream
.of(commaSeparated.split(","))
.mapToInt(Integer::parseInt)
.toArray();
You can combine this with varargs to get what you want:
public static void main (String[] args) {
String commaSeparated = "0,1,2,3,4,5,6,7,8,9";
int[] arguments =
Stream
.of(commaSeparated.split(","))
.mapToInt(Integer::parseInt)
.toArray();
pages(arguments);
}
public static void pages(int... numbers) {
System.out.println("numbers: " + Arrays.toString(numbers));
}
Output will be:
numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Note: of course varargs isn't needed in this specific case, it's useful only if you plan to call the same method with a variable number of int
arguments, otherwise you can just change its signature to pages(int[] numbers)
.