2

I'm stuck. I'm trying to remove the last comma at the back of the output but I just don't know how.

123, 97, 88, 99, 200, 50,

This is my code below, while checking for highest number in array.

public static void main(String[] args) {
    int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};

    for (int i : array) {
        if (i >= 50) {
            System.out.print(i + ", ");
        }
    }
    System.out.println();
}
2
  • after the loop, just do a substring Commented May 24, 2022 at 7:13
  • Let me just share my previous answer regarding StringJoiner Commented May 24, 2022 at 7:16

4 Answers 4

8

One workaround here would be to prepend a comma to all but the first element in the array.

public static void main(String[] args) {
    int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};

    for (int i=0; i < array.length; ++i) {
        if (i > 0 && array[i] >= 50) {
            System.out.print(", ");
        }
        if (array[i] >= 50) {
            System.out.print(array[i]);
        }
    }
    System.out.println();
}

This prints:

97, 123, 88, 200, 50, 99

Edit:

For brevity, and for the sake of using Java's internal APIs which already handle your requirement, we could just use Arrays.toString directly:

int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};
String output = Arrays.toString(array).replaceAll("^\\[|\\]$", "");
System.out.println(output);
// 4, 97, 123, 49, 88, 200, 50, 13, 26, 99
Sign up to request clarification or add additional context in comments.

2 Comments

I unroll your loop and understand your method but what if I also want to select only certain index more than or less than a value. E.g. for index more than 100, it will print only 123, 200.
@JohnNg That's not the question you asked, but in any case, my general answer should work with arrays of any size. So if you feed in the array you want to view, it should still work.
3

To fix your code, I add a flag that indicates the first array element printed. For all subsequent array elements printed, I prepend the delimiter.

    int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};
    boolean first = true;
    for (int i : array) {
        if (i >= 50) {
            if (first) {
                first = false;
            }
            else {
                System.out.print(", ");
            }
            System.out.print(i);
        }
    }
    System.out.println();

Running the above code prints the following:

97, 123, 88, 200, 50, 99

Alternatively, you can use streams:

public static void main(String[] args) {
    int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};
    String result = Arrays.stream(array)
                          .filter(i -> i >= 50)
                          .boxed()
                          .map(i -> i.toString())
                          .collect(Collectors.joining(", "));
    System.out.println(result);
  • Arrays.stream returns a stream of int
  • filter keeps only those elements in the array that are >= 50
  • boxed converts int (primitive) to Integer (object)
  • map converts Integer to String
  • Collectors.joining concatenates all elements in stream and separates each element with a comma followed by a space, i.e. ,

Running the above code prints the following:

97, 123, 88, 200, 50, 99

5 Comments

Thanks! this also gave me new insights and mechanics to the solution of the problem.
Right my bad, the output was the same and the solution did just that. Thanks mate!
Haha, I'm still a bit incompetent at the moment since I'm still learning Java after a few weeks. However, I'll take my time to process and note all this methods.
@JohnNg I recommend Oracle's Java tutorial
0

Another solution, and to keep for-each statement:

public static void main(String[] args) {
    int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};

    boolean found = false;
    for (int i : array) {
        if (found) {
            System.out.print(", ");
        } else {
            found = true;
        }
        System.out.print(i);
    }
    System.out.println();
}

Comments

0

Here is another way you can do this. It makes use of the StringBuilder class:

int[] array = {4, 97, 123, 49, 88, 200, 50, 13, 26, 99};

StringBuilder sb = new StringBuilder("");
for (int i : array) {
    if (i >= 50) {
        if (!sb.toString().isEmpty()) {
            sb.append(", ");
        }
        sb.append(i);
    }
}
System.out.println(sb.toString());

Output to console window will be:

97, 123, 88, 200, 50, 99

Comments

Your Answer

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.