Reputation: 81
I'm trying to remove the last comma from my print method. Is there a way to do this? Thank you.
public void print() {
for(int i = 0; i < M.length; i++){
for(int j = 0; j < M[i].length; j++){
System.out.print(M[i][j] + ",");
}
System.out.println();
}
}
Output:
1.5,2.0,3.0,
3.0,2.5,4.0,
2.5,4.0,2.5,
I ended up adding an "if" statement to solve the same problem. Here is how I ended up doing it:
public void print() {
for(int i = 0; i < M.length; i++){
for(int j = 0; j < M[i].length; j++){
if(j == M[i].length-1){
System.out.print(M[i][j] +" ");
}
else{
System.out.print(M[i][j] + ",");
}
}
System.out.println();
}
}
Output:
1.5,2.0,3.0
3.0,2.5,4.0
2.5,4.0,2.5
Upvotes: 3
Views: 1764
Reputation: 21
You should use an if else
statement in your second for
loop.
And instead of putting the comma at the back, it's better to put it at the front.
The sample code looks like this:
public void print(){
int[][] M = {{1,3},{2,3,4},{5,6},{5,2}};
for(int i = 0; i < M.length; i++){
for(int j = 0; j < M[i].length; j++){
if (j==0)
System.out.print(M[i][j]);
else
System.out.print(","+M[i][j]);
}
System.out.println();
}
}
The code will output
1,3
2,3,4
5,6
5,2
Upvotes: 2
Reputation: 16
Here's a cool Java 8 way of doing it if you like streams. Works with arrays of int/double :D
Arrays.stream(M)
.map(x -> Arrays.stream(x)
.mapToObj(String::valueOf)
.collect(Collectors.joining(",")))
.forEach(System.out::println);
Upvotes: 0
Reputation: 189
You can try this
public void print() {
for(int i = 0; i < M.length; i++){
for(int j = 0; j < M[i].length; j++){
System.out.print(M[i][j]);
if (i < M.length-1 || j < M[i].length - 1) {
System.out.print(",")
}
}
System.out.println();
}
}
Basically it checks if the element is the last in the array and if not, it prints the comma. if it is it doesn't print the comma. Hope this helps
Upvotes: 1
Reputation: 3020
You can use ","
before the item instead of after, and simply don't print it before the first item:
public void print() {
for (int i = 0; i < M.length; i++) {
if (M[i].length != 0) {
System.out.print(M[i][0]);
}
for(int j = 1; j < M[i].length; j++) {
System.out.print("," + M[i][j]);
}
System.out.println();
}
}
You might also want to store the row in a variable so that you don't keep accessing the i
th element of M
repeatedly:
public void print() {
for (int i = 0; i < M.length; i++) {
final type[] row = M[i];
if (row.length != 0) {
System.out.print(row[0]);
}
for(int j = 1; j < row.length; j++) {
System.out.print("," + row[j]);
}
System.out.println();
}
}
Upvotes: 0
Reputation: 7152
I often use this idiom for this problem -- make a variable comma
special (""
) for the first time, and then restore it to the normal state (", "
) for the second time and onward:
public void print() {
for(int i = 0; i < M.length; i++){
String comma = ""; // first time special
for(int j = 0; j < M[i].length; j++){
System.out.print(comma + M[i][j]);
comma = ","; // second time onward
}
System.out.println();
}
}
Upvotes: 3
Reputation: 1017
In your second for loop, add an if statement that checks to see if it's the last item of the list. If it is, omit the comma. Else, put the comma.
Upvotes: 1