0

So basically this is how my code looked like

public static void printPrime(int[] arr)
    {
        int len = arr.length;

        for(int i = 0; i < len; i++)
        {
            int c = countFactor(arr[i]);

            if(c == 2)
            {
             System.out.print(arr[i] + ",");
            }

        }
    }

So the output did have the 'comma' in the end. I tried looking around to remove the last comma some answers say to print last element separately but that can only happen when output depends on the for loop and not the if condition. But as you can see I don't know how many elements I am going to get from the if condition. Only two things I can think of, to add another loop or use String then substr to output.

So I converted it to String

public static void printPrime(int[] arr)
    {
        int len = arr.length;
        String str = "";
        for(int i = 0; i < len; i++)
        {
            int c = countFactor(arr[i]);

            if(c == 2)
            {
              str = str + arr[i] + ",";
            }

        }
        str = str.substring(0, str.length()-1);
        System.out.println(str);
    }

My question is about knowing the optimum way (converting to string then substringing it?) for similar questions or could there be a better way as well? That I seem to be missing.

Shad
  • 1,185
  • 1
  • 12
  • 27
  • 1
    you can use `String.join` – Viet Aug 25 '17 at 08:20
  • Yup. Collect the values from `countFactor()` somewhere and then use `String.join()` on them. For extra credit use `IntStream` to stream the array so you don't need to create extra space for the values of `countFactor()`. – Kayaman Aug 25 '17 at 08:24
  • 1
    @Shad it is not a good idea to use string concatenation in a loop: if you're going to build a string, use a `StringBuilder` to avoid constructing lots of intermediate strings. – Andy Turner Aug 25 '17 at 08:25
  • @Kayaman I am not actually directly using values from the `countFactor()` and outputting them. I am operating on integer array elements as you can see. – Shad Aug 25 '17 at 09:09
  • @Kayaman And since I should be using String.join() first of all, I suppose I should be converting all int elements from the array into String.Adding to Single String won't do because all elements added to that string would make it one element string. String array won't do since I don't know the size before hand. So I suppose I am left with using String ArrayList? And then finally use String.join on it. – Shad Aug 25 '17 at 09:26
  • Yeah something like that, `List` would work jus fine too. – Kayaman Aug 25 '17 at 09:31
  • huh? But String.join() does not permit using `List`! – Shad Aug 25 '17 at 09:42
  • Ah, what a shame. I guess they wanted to avoid accidents. – Kayaman Aug 25 '17 at 10:21

2 Answers2

2

You don't have to construct a string. Consider the following slight tweaks:

public static void printPrime(int[] arr)
{
    int len = arr.length;
    String sep = "";    // HERE
    for(int i = 0; i < len; i++)
    {
        int c = countFactor(arr[i]);

        if(c == 2)
        {
         System.out.print(sep);  // HERE
         sep = ",";
         System.out.print(arr[i]);
        }

    }
}

Print the delimiter first, and store its value in a variable: the first time it's printed, it will print the empty string. Thereafter, it prints the comma.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

Whatever means you use should operate correctly for an empty array (length 0), a singleton array (length 1) and a long array (a large length).

Adding the comma then removing it requires special case handling for the empty array case. So you must have conditional code (an if statement) whatever you do.

Raedwald
  • 46,613
  • 43
  • 151
  • 237