Converting the comma seprated numeric string into int array











up vote
3
down vote

favorite
1












//convert the comma separated numeric string into the  array of int. 
public class HelloWorld
{
public static void main(String args)
{
// line is the input which have the comma separated number
String line = "1,2,3,1,2,2,1,2,3,";
// 1 > split
String inputNumber = line.split(",");
// 1.1 > declear int array
int number = new int[10];
// 2 > convert the String into int and save it in int array.
for(int i=0; i<inputNumber.length;i++){
number[i]=Integer.parseInt(inputNumber[i]);
}
}

}


Is there a more efficient solution to achieve the same result?










share|improve this question




















  • 2




    Duplicated from SO: stackoverflow.com/q/35764996/3207406
    – oliverpool
    Mar 3 '16 at 7:12















up vote
3
down vote

favorite
1












//convert the comma separated numeric string into the  array of int. 
public class HelloWorld
{
public static void main(String args)
{
// line is the input which have the comma separated number
String line = "1,2,3,1,2,2,1,2,3,";
// 1 > split
String inputNumber = line.split(",");
// 1.1 > declear int array
int number = new int[10];
// 2 > convert the String into int and save it in int array.
for(int i=0; i<inputNumber.length;i++){
number[i]=Integer.parseInt(inputNumber[i]);
}
}

}


Is there a more efficient solution to achieve the same result?










share|improve this question




















  • 2




    Duplicated from SO: stackoverflow.com/q/35764996/3207406
    – oliverpool
    Mar 3 '16 at 7:12













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





//convert the comma separated numeric string into the  array of int. 
public class HelloWorld
{
public static void main(String args)
{
// line is the input which have the comma separated number
String line = "1,2,3,1,2,2,1,2,3,";
// 1 > split
String inputNumber = line.split(",");
// 1.1 > declear int array
int number = new int[10];
// 2 > convert the String into int and save it in int array.
for(int i=0; i<inputNumber.length;i++){
number[i]=Integer.parseInt(inputNumber[i]);
}
}

}


Is there a more efficient solution to achieve the same result?










share|improve this question















//convert the comma separated numeric string into the  array of int. 
public class HelloWorld
{
public static void main(String args)
{
// line is the input which have the comma separated number
String line = "1,2,3,1,2,2,1,2,3,";
// 1 > split
String inputNumber = line.split(",");
// 1.1 > declear int array
int number = new int[10];
// 2 > convert the String into int and save it in int array.
for(int i=0; i<inputNumber.length;i++){
number[i]=Integer.parseInt(inputNumber[i]);
}
}

}


Is there a more efficient solution to achieve the same result?







java strings array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 3 '16 at 17:23









SuperBiasedMan

11.8k52660




11.8k52660










asked Mar 3 '16 at 7:04









Prabhat Yadav

74115




74115








  • 2




    Duplicated from SO: stackoverflow.com/q/35764996/3207406
    – oliverpool
    Mar 3 '16 at 7:12














  • 2




    Duplicated from SO: stackoverflow.com/q/35764996/3207406
    – oliverpool
    Mar 3 '16 at 7:12








2




2




Duplicated from SO: stackoverflow.com/q/35764996/3207406
– oliverpool
Mar 3 '16 at 7:12




Duplicated from SO: stackoverflow.com/q/35764996/3207406
– oliverpool
Mar 3 '16 at 7:12










3 Answers
3






active

oldest

votes

















up vote
4
down vote













Basic improvements




  1. Instead of setting the size of the int array to 10, it would be better to derive the right size from the size of String array

  2. Instead of int number the more conventional way to write is int number

  3. For structures that contain multiple values it's more natural to give plural names, for example "number" -> "numbers" for an array of numbers

  4. The variable names are very poor in general, and should be improved to better reflect their purpose, making the code easier to understand


Something like this:



String line = "1,2,3,1,2,2,1,2,3,";
String parts = line.split(",");
int ints = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
ints[i] = Integer.parseInt(parts[i]);
}


Split to logical steps



It's good to get into the habit of decomposing tasks to their small steps. That is, instead of having all the logical steps in a single main method, it would be better to split to multiple functions, for example:



static int toIntArray(String arr) {
int ints = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
ints[i] = Integer.parseInt(arr[i]);
}
return ints;
}

static int parseLineToIntArray(String line) {
return toIntArray(line.split(","));
}

public static void main(String args) {
String line = "1,2,3,1,2,2,1,2,3,";
System.out.println(Arrays.toString(parseLineToIntArray(line)));
}





share|improve this answer






























    up vote
    3
    down vote













    You code is not properly indented and IMO your comments don't add any value. You could create a separate function instead of putting everything in the main function. Also you could add an extra parameter so you can specify the delimiter instead of always being ",".



    Also if you can use java 8 this becomes even more trivial:



    public static int toIntArray(String input, String delimiter) {

    return Arrays.stream(input.split(delimiter))
    .mapToInt(Integer::parseInt)
    .toArray();
    }





    share|improve this answer




























      up vote
      0
      down vote













      Here is the solution of your problem which splits the comma separated values and also converts it in to Integer Array



      String line = "1,2,3,1,2,2,1,2,3,";

      //If you want only unique values
      Set<Integer> set = Stream.of(line.split(",")).map(Integer::parseInt).collect(Collectors.toSet());
      //If you want all values
      List<Integer> list = Stream.of(documentMailIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());

      int uniqueNumbers = set.toArray();
      int allNumbers = list.toArray();


      It is more faster then conventional solution because it uses multi-core functionality at hardware level.






      share|improve this answer





















      • Extremely overkill for the purpose of learning how to use Java on a basic level, and unfortunately without accompanying explanation. Why is the second line documentMailIds instead of line? Also doesn't add much to @MAG 's answer, and there is no guarantee that this will be executed in parallel. Just using Stream doesn't allow parallel computation, you want a specific parallel Stream using the parallelStream method on the Stream you got from Stream.of.
        – Tamoghna Chowdhury
        Nov 15 at 12:23











      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      });
      });
      }, "mathjax-editing");

      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: "196"
      };
      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',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2fcodereview.stackexchange.com%2fquestions%2f121756%2fconverting-the-comma-seprated-numeric-string-into-int-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      Basic improvements




      1. Instead of setting the size of the int array to 10, it would be better to derive the right size from the size of String array

      2. Instead of int number the more conventional way to write is int number

      3. For structures that contain multiple values it's more natural to give plural names, for example "number" -> "numbers" for an array of numbers

      4. The variable names are very poor in general, and should be improved to better reflect their purpose, making the code easier to understand


      Something like this:



      String line = "1,2,3,1,2,2,1,2,3,";
      String parts = line.split(",");
      int ints = new int[parts.length];
      for (int i = 0; i < parts.length; i++) {
      ints[i] = Integer.parseInt(parts[i]);
      }


      Split to logical steps



      It's good to get into the habit of decomposing tasks to their small steps. That is, instead of having all the logical steps in a single main method, it would be better to split to multiple functions, for example:



      static int toIntArray(String arr) {
      int ints = new int[arr.length];
      for (int i = 0; i < arr.length; i++) {
      ints[i] = Integer.parseInt(arr[i]);
      }
      return ints;
      }

      static int parseLineToIntArray(String line) {
      return toIntArray(line.split(","));
      }

      public static void main(String args) {
      String line = "1,2,3,1,2,2,1,2,3,";
      System.out.println(Arrays.toString(parseLineToIntArray(line)));
      }





      share|improve this answer



























        up vote
        4
        down vote













        Basic improvements




        1. Instead of setting the size of the int array to 10, it would be better to derive the right size from the size of String array

        2. Instead of int number the more conventional way to write is int number

        3. For structures that contain multiple values it's more natural to give plural names, for example "number" -> "numbers" for an array of numbers

        4. The variable names are very poor in general, and should be improved to better reflect their purpose, making the code easier to understand


        Something like this:



        String line = "1,2,3,1,2,2,1,2,3,";
        String parts = line.split(",");
        int ints = new int[parts.length];
        for (int i = 0; i < parts.length; i++) {
        ints[i] = Integer.parseInt(parts[i]);
        }


        Split to logical steps



        It's good to get into the habit of decomposing tasks to their small steps. That is, instead of having all the logical steps in a single main method, it would be better to split to multiple functions, for example:



        static int toIntArray(String arr) {
        int ints = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
        ints[i] = Integer.parseInt(arr[i]);
        }
        return ints;
        }

        static int parseLineToIntArray(String line) {
        return toIntArray(line.split(","));
        }

        public static void main(String args) {
        String line = "1,2,3,1,2,2,1,2,3,";
        System.out.println(Arrays.toString(parseLineToIntArray(line)));
        }





        share|improve this answer

























          up vote
          4
          down vote










          up vote
          4
          down vote









          Basic improvements




          1. Instead of setting the size of the int array to 10, it would be better to derive the right size from the size of String array

          2. Instead of int number the more conventional way to write is int number

          3. For structures that contain multiple values it's more natural to give plural names, for example "number" -> "numbers" for an array of numbers

          4. The variable names are very poor in general, and should be improved to better reflect their purpose, making the code easier to understand


          Something like this:



          String line = "1,2,3,1,2,2,1,2,3,";
          String parts = line.split(",");
          int ints = new int[parts.length];
          for (int i = 0; i < parts.length; i++) {
          ints[i] = Integer.parseInt(parts[i]);
          }


          Split to logical steps



          It's good to get into the habit of decomposing tasks to their small steps. That is, instead of having all the logical steps in a single main method, it would be better to split to multiple functions, for example:



          static int toIntArray(String arr) {
          int ints = new int[arr.length];
          for (int i = 0; i < arr.length; i++) {
          ints[i] = Integer.parseInt(arr[i]);
          }
          return ints;
          }

          static int parseLineToIntArray(String line) {
          return toIntArray(line.split(","));
          }

          public static void main(String args) {
          String line = "1,2,3,1,2,2,1,2,3,";
          System.out.println(Arrays.toString(parseLineToIntArray(line)));
          }





          share|improve this answer














          Basic improvements




          1. Instead of setting the size of the int array to 10, it would be better to derive the right size from the size of String array

          2. Instead of int number the more conventional way to write is int number

          3. For structures that contain multiple values it's more natural to give plural names, for example "number" -> "numbers" for an array of numbers

          4. The variable names are very poor in general, and should be improved to better reflect their purpose, making the code easier to understand


          Something like this:



          String line = "1,2,3,1,2,2,1,2,3,";
          String parts = line.split(",");
          int ints = new int[parts.length];
          for (int i = 0; i < parts.length; i++) {
          ints[i] = Integer.parseInt(parts[i]);
          }


          Split to logical steps



          It's good to get into the habit of decomposing tasks to their small steps. That is, instead of having all the logical steps in a single main method, it would be better to split to multiple functions, for example:



          static int toIntArray(String arr) {
          int ints = new int[arr.length];
          for (int i = 0; i < arr.length; i++) {
          ints[i] = Integer.parseInt(arr[i]);
          }
          return ints;
          }

          static int parseLineToIntArray(String line) {
          return toIntArray(line.split(","));
          }

          public static void main(String args) {
          String line = "1,2,3,1,2,2,1,2,3,";
          System.out.println(Arrays.toString(parseLineToIntArray(line)));
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 3 '16 at 16:48

























          answered Mar 3 '16 at 7:42









          janos

          96.5k12122349




          96.5k12122349
























              up vote
              3
              down vote













              You code is not properly indented and IMO your comments don't add any value. You could create a separate function instead of putting everything in the main function. Also you could add an extra parameter so you can specify the delimiter instead of always being ",".



              Also if you can use java 8 this becomes even more trivial:



              public static int toIntArray(String input, String delimiter) {

              return Arrays.stream(input.split(delimiter))
              .mapToInt(Integer::parseInt)
              .toArray();
              }





              share|improve this answer

























                up vote
                3
                down vote













                You code is not properly indented and IMO your comments don't add any value. You could create a separate function instead of putting everything in the main function. Also you could add an extra parameter so you can specify the delimiter instead of always being ",".



                Also if you can use java 8 this becomes even more trivial:



                public static int toIntArray(String input, String delimiter) {

                return Arrays.stream(input.split(delimiter))
                .mapToInt(Integer::parseInt)
                .toArray();
                }





                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  You code is not properly indented and IMO your comments don't add any value. You could create a separate function instead of putting everything in the main function. Also you could add an extra parameter so you can specify the delimiter instead of always being ",".



                  Also if you can use java 8 this becomes even more trivial:



                  public static int toIntArray(String input, String delimiter) {

                  return Arrays.stream(input.split(delimiter))
                  .mapToInt(Integer::parseInt)
                  .toArray();
                  }





                  share|improve this answer












                  You code is not properly indented and IMO your comments don't add any value. You could create a separate function instead of putting everything in the main function. Also you could add an extra parameter so you can specify the delimiter instead of always being ",".



                  Also if you can use java 8 this becomes even more trivial:



                  public static int toIntArray(String input, String delimiter) {

                  return Arrays.stream(input.split(delimiter))
                  .mapToInt(Integer::parseInt)
                  .toArray();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 '16 at 20:23









                  MAG

                  2,399524




                  2,399524






















                      up vote
                      0
                      down vote













                      Here is the solution of your problem which splits the comma separated values and also converts it in to Integer Array



                      String line = "1,2,3,1,2,2,1,2,3,";

                      //If you want only unique values
                      Set<Integer> set = Stream.of(line.split(",")).map(Integer::parseInt).collect(Collectors.toSet());
                      //If you want all values
                      List<Integer> list = Stream.of(documentMailIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());

                      int uniqueNumbers = set.toArray();
                      int allNumbers = list.toArray();


                      It is more faster then conventional solution because it uses multi-core functionality at hardware level.






                      share|improve this answer





















                      • Extremely overkill for the purpose of learning how to use Java on a basic level, and unfortunately without accompanying explanation. Why is the second line documentMailIds instead of line? Also doesn't add much to @MAG 's answer, and there is no guarantee that this will be executed in parallel. Just using Stream doesn't allow parallel computation, you want a specific parallel Stream using the parallelStream method on the Stream you got from Stream.of.
                        – Tamoghna Chowdhury
                        Nov 15 at 12:23















                      up vote
                      0
                      down vote













                      Here is the solution of your problem which splits the comma separated values and also converts it in to Integer Array



                      String line = "1,2,3,1,2,2,1,2,3,";

                      //If you want only unique values
                      Set<Integer> set = Stream.of(line.split(",")).map(Integer::parseInt).collect(Collectors.toSet());
                      //If you want all values
                      List<Integer> list = Stream.of(documentMailIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());

                      int uniqueNumbers = set.toArray();
                      int allNumbers = list.toArray();


                      It is more faster then conventional solution because it uses multi-core functionality at hardware level.






                      share|improve this answer





















                      • Extremely overkill for the purpose of learning how to use Java on a basic level, and unfortunately without accompanying explanation. Why is the second line documentMailIds instead of line? Also doesn't add much to @MAG 's answer, and there is no guarantee that this will be executed in parallel. Just using Stream doesn't allow parallel computation, you want a specific parallel Stream using the parallelStream method on the Stream you got from Stream.of.
                        – Tamoghna Chowdhury
                        Nov 15 at 12:23













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Here is the solution of your problem which splits the comma separated values and also converts it in to Integer Array



                      String line = "1,2,3,1,2,2,1,2,3,";

                      //If you want only unique values
                      Set<Integer> set = Stream.of(line.split(",")).map(Integer::parseInt).collect(Collectors.toSet());
                      //If you want all values
                      List<Integer> list = Stream.of(documentMailIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());

                      int uniqueNumbers = set.toArray();
                      int allNumbers = list.toArray();


                      It is more faster then conventional solution because it uses multi-core functionality at hardware level.






                      share|improve this answer












                      Here is the solution of your problem which splits the comma separated values and also converts it in to Integer Array



                      String line = "1,2,3,1,2,2,1,2,3,";

                      //If you want only unique values
                      Set<Integer> set = Stream.of(line.split(",")).map(Integer::parseInt).collect(Collectors.toSet());
                      //If you want all values
                      List<Integer> list = Stream.of(documentMailIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());

                      int uniqueNumbers = set.toArray();
                      int allNumbers = list.toArray();


                      It is more faster then conventional solution because it uses multi-core functionality at hardware level.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 7 at 7:25









                      Sunil Kanjar

                      1094




                      1094












                      • Extremely overkill for the purpose of learning how to use Java on a basic level, and unfortunately without accompanying explanation. Why is the second line documentMailIds instead of line? Also doesn't add much to @MAG 's answer, and there is no guarantee that this will be executed in parallel. Just using Stream doesn't allow parallel computation, you want a specific parallel Stream using the parallelStream method on the Stream you got from Stream.of.
                        – Tamoghna Chowdhury
                        Nov 15 at 12:23


















                      • Extremely overkill for the purpose of learning how to use Java on a basic level, and unfortunately without accompanying explanation. Why is the second line documentMailIds instead of line? Also doesn't add much to @MAG 's answer, and there is no guarantee that this will be executed in parallel. Just using Stream doesn't allow parallel computation, you want a specific parallel Stream using the parallelStream method on the Stream you got from Stream.of.
                        – Tamoghna Chowdhury
                        Nov 15 at 12:23
















                      Extremely overkill for the purpose of learning how to use Java on a basic level, and unfortunately without accompanying explanation. Why is the second line documentMailIds instead of line? Also doesn't add much to @MAG 's answer, and there is no guarantee that this will be executed in parallel. Just using Stream doesn't allow parallel computation, you want a specific parallel Stream using the parallelStream method on the Stream you got from Stream.of.
                      – Tamoghna Chowdhury
                      Nov 15 at 12:23




                      Extremely overkill for the purpose of learning how to use Java on a basic level, and unfortunately without accompanying explanation. Why is the second line documentMailIds instead of line? Also doesn't add much to @MAG 's answer, and there is no guarantee that this will be executed in parallel. Just using Stream doesn't allow parallel computation, you want a specific parallel Stream using the parallelStream method on the Stream you got from Stream.of.
                      – Tamoghna Chowdhury
                      Nov 15 at 12:23


















                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f121756%2fconverting-the-comma-seprated-numeric-string-into-int-array%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

                      Сан-Квентин

                      8-я гвардейская общевойсковая армия

                      Алькесар