the question was write a program the will do the comma delimited list of numbers,grouping the numbers into a range when they are sequential. given the Input:
1,2,6,7,8,9,12,13,14,15,21,22,23,24,25,31
and expected output:
"[[1],[2], [6,7,8], [13,14,15], [21-25], [32]]"
and i wrote a simple code like this
public static void main(String []args){
String input = "1,3,6,7,8,9,12,13,14,15,21,22,23,24,25,31";
String []num = input .split(",");
String temp = "[";
int min = 1;
for(int i =0; i <num.length;i++){
if(num[1] == num[0]){
temp = temp + num[i]+"]";
}else if (num[min+1] != num[i]){
temp = temp + "," +num[i];
}else{
temp = "]";
}
System.out.print(temp);
}
}
when i run this code it give me the following output:
[,1[,1,3]],7],7,8],7,8,9],7,8,9,12],7,8,9,12,13],7,8,9,12,13,14],7,8,9,12,13,14,15],7,8,9,12,13,14,15,21],7,8,9,12,13,14,15,21,22],7,8,9,12,13,14,15,21,22,23],7,8,9,12,13,14,15,21,22,23,24],7,8,9,12,13,14,15,21,22,23,24,25],7,8,9,12,13,14,15,21,22,23,24,25,31]