Get 2 integers separated by space from string









up vote
-2
down vote

favorite












Let's say I have an input formatted as "a b" and I want to extract a and b. For example, if my input is 10 15 I want to set a as 10 and b as 15. I usually do this by using StringTokenizer like so:



StringTokenizer st = new StringTokenizer(str, " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());


Is there any way where I can do this in a much shorter piece of code?










share|improve this question





















  • That is short and simple and easy to read. How would you expect to 1) parse a string 2) assign values to 2 variable in less that 3 statements? Even if you could do it in less statements it doesn't mean you should since the code will become unreadable and much harder to debug.
    – camickr
    Nov 12 at 3:11







  • 2




    If you're looking for short, unreadable code, choose something other than Java
    – Jacob G.
    Nov 12 at 3:12














up vote
-2
down vote

favorite












Let's say I have an input formatted as "a b" and I want to extract a and b. For example, if my input is 10 15 I want to set a as 10 and b as 15. I usually do this by using StringTokenizer like so:



StringTokenizer st = new StringTokenizer(str, " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());


Is there any way where I can do this in a much shorter piece of code?










share|improve this question





















  • That is short and simple and easy to read. How would you expect to 1) parse a string 2) assign values to 2 variable in less that 3 statements? Even if you could do it in less statements it doesn't mean you should since the code will become unreadable and much harder to debug.
    – camickr
    Nov 12 at 3:11







  • 2




    If you're looking for short, unreadable code, choose something other than Java
    – Jacob G.
    Nov 12 at 3:12












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Let's say I have an input formatted as "a b" and I want to extract a and b. For example, if my input is 10 15 I want to set a as 10 and b as 15. I usually do this by using StringTokenizer like so:



StringTokenizer st = new StringTokenizer(str, " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());


Is there any way where I can do this in a much shorter piece of code?










share|improve this question













Let's say I have an input formatted as "a b" and I want to extract a and b. For example, if my input is 10 15 I want to set a as 10 and b as 15. I usually do this by using StringTokenizer like so:



StringTokenizer st = new StringTokenizer(str, " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());


Is there any way where I can do this in a much shorter piece of code?







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 3:09









candymint

33910




33910











  • That is short and simple and easy to read. How would you expect to 1) parse a string 2) assign values to 2 variable in less that 3 statements? Even if you could do it in less statements it doesn't mean you should since the code will become unreadable and much harder to debug.
    – camickr
    Nov 12 at 3:11







  • 2




    If you're looking for short, unreadable code, choose something other than Java
    – Jacob G.
    Nov 12 at 3:12
















  • That is short and simple and easy to read. How would you expect to 1) parse a string 2) assign values to 2 variable in less that 3 statements? Even if you could do it in less statements it doesn't mean you should since the code will become unreadable and much harder to debug.
    – camickr
    Nov 12 at 3:11







  • 2




    If you're looking for short, unreadable code, choose something other than Java
    – Jacob G.
    Nov 12 at 3:12















That is short and simple and easy to read. How would you expect to 1) parse a string 2) assign values to 2 variable in less that 3 statements? Even if you could do it in less statements it doesn't mean you should since the code will become unreadable and much harder to debug.
– camickr
Nov 12 at 3:11





That is short and simple and easy to read. How would you expect to 1) parse a string 2) assign values to 2 variable in less that 3 statements? Even if you could do it in less statements it doesn't mean you should since the code will become unreadable and much harder to debug.
– camickr
Nov 12 at 3:11





2




2




If you're looking for short, unreadable code, choose something other than Java
– Jacob G.
Nov 12 at 3:12




If you're looking for short, unreadable code, choose something other than Java
– Jacob G.
Nov 12 at 3:12












1 Answer
1






active

oldest

votes

















up vote
1
down vote













You could convert it to an int by using String.split(String) and then streaming that to an IntStream by mapping. Like,



int vals = Stream.of(str.split("\s+")).mapToInt(Integer::parseInt).toArray();
int a = vals[0], b = vals[1];


And you can use a comma to separate assignments. Of course, your original version code use that trick to eliminate a line. However, now we can eliminate a and b by using vals[0] and vals[1]. Without further context I wouldn't necessarily recommend that.






share|improve this answer




















    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/3.0/"u003ecc by-sa 3.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%2f53255490%2fget-2-integers-separated-by-space-from-string%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You could convert it to an int by using String.split(String) and then streaming that to an IntStream by mapping. Like,



    int vals = Stream.of(str.split("\s+")).mapToInt(Integer::parseInt).toArray();
    int a = vals[0], b = vals[1];


    And you can use a comma to separate assignments. Of course, your original version code use that trick to eliminate a line. However, now we can eliminate a and b by using vals[0] and vals[1]. Without further context I wouldn't necessarily recommend that.






    share|improve this answer
























      up vote
      1
      down vote













      You could convert it to an int by using String.split(String) and then streaming that to an IntStream by mapping. Like,



      int vals = Stream.of(str.split("\s+")).mapToInt(Integer::parseInt).toArray();
      int a = vals[0], b = vals[1];


      And you can use a comma to separate assignments. Of course, your original version code use that trick to eliminate a line. However, now we can eliminate a and b by using vals[0] and vals[1]. Without further context I wouldn't necessarily recommend that.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        You could convert it to an int by using String.split(String) and then streaming that to an IntStream by mapping. Like,



        int vals = Stream.of(str.split("\s+")).mapToInt(Integer::parseInt).toArray();
        int a = vals[0], b = vals[1];


        And you can use a comma to separate assignments. Of course, your original version code use that trick to eliminate a line. However, now we can eliminate a and b by using vals[0] and vals[1]. Without further context I wouldn't necessarily recommend that.






        share|improve this answer












        You could convert it to an int by using String.split(String) and then streaming that to an IntStream by mapping. Like,



        int vals = Stream.of(str.split("\s+")).mapToInt(Integer::parseInt).toArray();
        int a = vals[0], b = vals[1];


        And you can use a comma to separate assignments. Of course, your original version code use that trick to eliminate a line. However, now we can eliminate a and b by using vals[0] and vals[1]. Without further context I wouldn't necessarily recommend that.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 3:12









        Elliott Frisch

        151k1389178




        151k1389178



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53255490%2fget-2-integers-separated-by-space-from-string%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto