-5

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]
Arc676
  • 4,445
  • 3
  • 28
  • 44
SmurtD
  • 9
  • 1
  • 6
  • `if(num[1] == num[0]){` looks wrong. Why are you always checking [0] and [1]? – Steve Smith Jun 14 '17 at 10:31
  • you have to compare strings with .equals not with ==, but it seems you have another problem not just this – Youcef LAIDANI Jun 14 '17 at 10:38
  • Your expected output looks really weird. Why `[1],[2]` and not `[1,2]`? Why `[21-25]` and not `[21,22,23,24,25]`? Or why `[13,14,15]` and not `[13-15]`? What are the actual rules? Furthermore, your expectation ends with `[32]` but your input ends with `31` - is it a typo? – Jaroslaw Pawlak Jun 14 '17 at 10:40
  • if(num[i] !=null){ i also did try it this way – SmurtD Jun 14 '17 at 10:42
  • my mistake is 3 not 2 – SmurtD Jun 14 '17 at 10:44
  • 5
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Jun 14 '17 at 10:45

1 Answers1

0

Is this a homework? Try at your best to solve homework yourself!

I think one of your problems is that you handle the numbers as strings, and the comparison of the strings only compares the references! Consider the following code:

String a =  new String("1");
String b = "1";

System.out.println("a: "+a+" b: "+b);
System.out.println(a==b);

The output is following:

a: 1 b: 1
false

Even though the strings have exactly the same contents, they do not refer to the same object, so you cannot be sure that the strings that you compare are equal! Strings should be compared with equals() method.

You should anyway first transform the numbers into int[] array by doing

String[] strs = input.split(",");

int[] ints = new int[strs.length()];
for(int i = 0;i<ints.length;i++){
    ints[i] = Integer.parseInt(strs[i]);
}

Or by using functional elements in java 8:

int[] integers = Arrays.stream(input.split(",")).map(Integer::parseInt).mapToInt(i->i).toArray();

Then you can compare them and be 100% sure the comparison is right.