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;
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
add a comment
|
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
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
add a comment
|
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
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
java string
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
add a comment
|
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
add a comment
|
2 Answers
2
active
oldest
votes
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)
.
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
add a comment
|
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]);
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
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
.
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
add a comment
|
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)
.
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
add a comment
|
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)
.
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)
.
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
add a comment
|
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
add a comment
|
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]);
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
add a comment
|
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]);
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
add a comment
|
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]);
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]);
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
add a comment
|
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
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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