Reputation: 35
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();
}
Upvotes: 2
Views: 1135
Reputation: 9202
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
Upvotes: 0
Reputation: 522797
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
Upvotes: 8
Reputation: 20924
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 >= 50boxed
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
Upvotes: 3
Reputation: 9658
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();
}
Upvotes: 0