HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Converting the comma seprated numeric string into int array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecommanumericarrayintoconvertingintstringseprated

Problem

//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?

Solution

Basic improvements

  • 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



  • Instead of int number[] the more conventional way to write is int[] number



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



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

Code Snippets

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

Context

StackExchange Code Review Q#121756, answer score: 5

Revisions (0)

No revisions yet.