How to pass a string with comma separated values as function parameters in JAVASplitting String and put it on int arrayIs Java “pass-by-reference” or “pass-by-value”?How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?How do I check if string contains substring?How to split a string in JavaHow do I convert a String to an int in Java?

2000s space film where an alien species has almost wiped out the human race in a war

How does a linear operator act on a bra?

Is there any way to land a rover on the Moon without using any thrusters?

Why is it called stateful and stateless firewall?

How can I discourage sharing internal API keys within a company?

What was the motivation for the invention of electric pianos?

Is there any reason to concentrate on the Thunderous Smite spell after using its effects?

What is the name of this Allen-head furniture fastener?

I was promised a work PC but still awaiting approval 3 months later so using my own laptop - Is it fair to ask employer for laptop insurance?

Usage of blank space in trade banner and text-positioning

Permutations in Disguise

Are there any “Third Order” acronyms used in space exploration?

If the gambler's fallacy is false, how do notions of "expected number" of events work?

The Planck constant for mathematicians

Why is my fire extinguisher emptied after one use?

Consonance v. Dissonance

Why don't airports use arresting gears to recover energy from landing passenger planes?

Parallel resistance in electric circuits

Is there a tool to measure the "maturity" of a code in Git?

How does a simple logistic regression model achieve a 92% classification accuracy on MNIST?

What do the French say for “Oh, you shouldn’t have”?

Are there successful ways of getting out of a REVERSE MORTGAGE?

Where is it? - The Google Earth Challenge Ep. 2

How to be sure services and researches offered by the University are not becoming cases of unfair competition?



How to pass a string with comma separated values as function parameters in JAVA


Splitting String and put it on int arrayIs Java “pass-by-reference” or “pass-by-value”?How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?How do I check if string contains substring?How to split a string in JavaHow do I convert a String to an int in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















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);









share|improve this question





















  • 3





    Possible duplicate of Splitting String and put it on int array

    – chris p bacon
    Mar 28 at 11:10











  • Use varargs, or pass in a collection.

    – Tim Biegeleisen
    Mar 28 at 11:11

















1















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);









share|improve this question





















  • 3





    Possible duplicate of Splitting String and put it on int array

    – chris p bacon
    Mar 28 at 11:10











  • Use varargs, or pass in a collection.

    – Tim Biegeleisen
    Mar 28 at 11:11













1












1








1








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);









share|improve this question
















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);






java string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 31 at 12:28







user3133925

















asked Mar 28 at 11:07









Isam H. AbuaqlainIsam H. Abuaqlain

367 bronze badges




367 bronze badges










  • 3





    Possible duplicate of Splitting String and put it on int array

    – chris p bacon
    Mar 28 at 11:10











  • Use varargs, or pass in a collection.

    – Tim Biegeleisen
    Mar 28 at 11:11












  • 3





    Possible duplicate of Splitting String and put it on int array

    – chris p bacon
    Mar 28 at 11:10











  • Use varargs, or pass in a collection.

    – Tim Biegeleisen
    Mar 28 at 11:11







3




3





Possible duplicate of Splitting String and put it on int array

– chris p bacon
Mar 28 at 11:10





Possible duplicate of Splitting String and put it on int array

– chris p bacon
Mar 28 at 11:10













Use varargs, or pass in a collection.

– Tim Biegeleisen
Mar 28 at 11:11





Use varargs, or pass in a collection.

– Tim Biegeleisen
Mar 28 at 11:11












2 Answers
2






active

oldest

votes


















3
















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).






share|improve this answer



























  • Thanks a lot, it works for me! as I'm new to programming and I'm using java in android programming. when I implement this solution it asked me to upgrade my language from v7 to v8 with lambda. what does it mean??

    – Isam H. Abuaqlain
    Mar 28 at 12:01












  • @IsamH.Abuaqlain It means that support for Streams was added starting from java 8 along with support for lambdas, so you can't use java 7 if you want to use Streams, you have to upgrade to java 8

    – BackSlash
    Mar 28 at 12:08












  • Thanks, I appreciate it!

    – Isam H. Abuaqlain
    Mar 28 at 12:10


















1
















Do this :



public class Test 
public static void main(String..args)
String pages = "1,2,3,6,9";
String[] pagesArr = pages.split(",");
pages(pagesArr);


static void pages(String... pagesArr)
int[] array = Arrays.asList(pagesArr).stream().mapToInt(Integer::parseInt).toArray();
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);








share|improve this answer






















  • 1





    Nice answer! I like the other one as well. It does not convert the array to a List first since a stream can be made from an array.

    – John Mercier
    Mar 28 at 12:24













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55396051%2fhow-to-pass-a-string-with-comma-separated-values-as-function-parameters-in-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3
















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).






share|improve this answer



























  • Thanks a lot, it works for me! as I'm new to programming and I'm using java in android programming. when I implement this solution it asked me to upgrade my language from v7 to v8 with lambda. what does it mean??

    – Isam H. Abuaqlain
    Mar 28 at 12:01












  • @IsamH.Abuaqlain It means that support for Streams was added starting from java 8 along with support for lambdas, so you can't use java 7 if you want to use Streams, you have to upgrade to java 8

    – BackSlash
    Mar 28 at 12:08












  • Thanks, I appreciate it!

    – Isam H. Abuaqlain
    Mar 28 at 12:10















3
















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).






share|improve this answer



























  • Thanks a lot, it works for me! as I'm new to programming and I'm using java in android programming. when I implement this solution it asked me to upgrade my language from v7 to v8 with lambda. what does it mean??

    – Isam H. Abuaqlain
    Mar 28 at 12:01












  • @IsamH.Abuaqlain It means that support for Streams was added starting from java 8 along with support for lambdas, so you can't use java 7 if you want to use Streams, you have to upgrade to java 8

    – BackSlash
    Mar 28 at 12:08












  • Thanks, I appreciate it!

    – Isam H. Abuaqlain
    Mar 28 at 12:10













3














3










3









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).






share|improve this answer















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).







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 12:19

























answered Mar 28 at 11:15









BackSlashBackSlash

17.1k17 gold badges63 silver badges107 bronze badges




17.1k17 gold badges63 silver badges107 bronze badges















  • Thanks a lot, it works for me! as I'm new to programming and I'm using java in android programming. when I implement this solution it asked me to upgrade my language from v7 to v8 with lambda. what does it mean??

    – Isam H. Abuaqlain
    Mar 28 at 12:01












  • @IsamH.Abuaqlain It means that support for Streams was added starting from java 8 along with support for lambdas, so you can't use java 7 if you want to use Streams, you have to upgrade to java 8

    – BackSlash
    Mar 28 at 12:08












  • Thanks, I appreciate it!

    – Isam H. Abuaqlain
    Mar 28 at 12:10

















  • Thanks a lot, it works for me! as I'm new to programming and I'm using java in android programming. when I implement this solution it asked me to upgrade my language from v7 to v8 with lambda. what does it mean??

    – Isam H. Abuaqlain
    Mar 28 at 12:01












  • @IsamH.Abuaqlain It means that support for Streams was added starting from java 8 along with support for lambdas, so you can't use java 7 if you want to use Streams, you have to upgrade to java 8

    – BackSlash
    Mar 28 at 12:08












  • Thanks, I appreciate it!

    – Isam H. Abuaqlain
    Mar 28 at 12:10
















Thanks a lot, it works for me! as I'm new to programming and I'm using java in android programming. when I implement this solution it asked me to upgrade my language from v7 to v8 with lambda. what does it mean??

– Isam H. Abuaqlain
Mar 28 at 12:01






Thanks a lot, it works for me! as I'm new to programming and I'm using java in android programming. when I implement this solution it asked me to upgrade my language from v7 to v8 with lambda. what does it mean??

– Isam H. Abuaqlain
Mar 28 at 12:01














@IsamH.Abuaqlain It means that support for Streams was added starting from java 8 along with support for lambdas, so you can't use java 7 if you want to use Streams, you have to upgrade to java 8

– BackSlash
Mar 28 at 12:08






@IsamH.Abuaqlain It means that support for Streams was added starting from java 8 along with support for lambdas, so you can't use java 7 if you want to use Streams, you have to upgrade to java 8

– BackSlash
Mar 28 at 12:08














Thanks, I appreciate it!

– Isam H. Abuaqlain
Mar 28 at 12:10





Thanks, I appreciate it!

– Isam H. Abuaqlain
Mar 28 at 12:10













1
















Do this :



public class Test 
public static void main(String..args)
String pages = "1,2,3,6,9";
String[] pagesArr = pages.split(",");
pages(pagesArr);


static void pages(String... pagesArr)
int[] array = Arrays.asList(pagesArr).stream().mapToInt(Integer::parseInt).toArray();
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);








share|improve this answer






















  • 1





    Nice answer! I like the other one as well. It does not convert the array to a List first since a stream can be made from an array.

    – John Mercier
    Mar 28 at 12:24















1
















Do this :



public class Test 
public static void main(String..args)
String pages = "1,2,3,6,9";
String[] pagesArr = pages.split(",");
pages(pagesArr);


static void pages(String... pagesArr)
int[] array = Arrays.asList(pagesArr).stream().mapToInt(Integer::parseInt).toArray();
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);








share|improve this answer






















  • 1





    Nice answer! I like the other one as well. It does not convert the array to a List first since a stream can be made from an array.

    – John Mercier
    Mar 28 at 12:24













1














1










1









Do this :



public class Test 
public static void main(String..args)
String pages = "1,2,3,6,9";
String[] pagesArr = pages.split(",");
pages(pagesArr);


static void pages(String... pagesArr)
int[] array = Arrays.asList(pagesArr).stream().mapToInt(Integer::parseInt).toArray();
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);








share|improve this answer















Do this :



public class Test 
public static void main(String..args)
String pages = "1,2,3,6,9";
String[] pagesArr = pages.split(",");
pages(pagesArr);


static void pages(String... pagesArr)
int[] array = Arrays.asList(pagesArr).stream().mapToInt(Integer::parseInt).toArray();
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);









share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 11:27

























answered Mar 28 at 11:22









Anish B.Anish B.

1,0901 gold badge5 silver badges18 bronze badges




1,0901 gold badge5 silver badges18 bronze badges










  • 1





    Nice answer! I like the other one as well. It does not convert the array to a List first since a stream can be made from an array.

    – John Mercier
    Mar 28 at 12:24












  • 1





    Nice answer! I like the other one as well. It does not convert the array to a List first since a stream can be made from an array.

    – John Mercier
    Mar 28 at 12:24







1




1





Nice answer! I like the other one as well. It does not convert the array to a List first since a stream can be made from an array.

– John Mercier
Mar 28 at 12:24





Nice answer! I like the other one as well. It does not convert the array to a List first since a stream can be made from an array.

– John Mercier
Mar 28 at 12:24


















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55396051%2fhow-to-pass-a-string-with-comma-separated-values-as-function-parameters-in-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript