3
\$\begingroup\$
//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?

SuperBiasedMan
13.5k5 gold badges37 silver badges62 bronze badges
asked Mar 3, 2016 at 7:04
\$\endgroup\$
1

3 Answers 3

5
\$\begingroup\$

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)));
}
answered Mar 3, 2016 at 7:42
\$\endgroup\$
3
\$\begingroup\$

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();
}
answered Mar 8, 2016 at 20:23
\$\endgroup\$
0
\$\begingroup\$

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.

answered Jun 7, 2018 at 7:25
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 15, 2018 at 12:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.