Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My code work fine exept that I dont know how to get rid of the last comma of the

ID: 3761913 • Letter: M

Question

My code work fine exept that I dont know how to get rid of the last comma of the printed array please help!!

This is my output:

Here is your array
5 , 6 , 7 , 8 ,
9 , 1 , 2 , 3 ,
4 , 5 , 6 , 7 ,

I want it to be

Here is your array
5 , 6 , 7 , 8
9 , 1 , 2 , 3
4 , 5 , 6 , 7


public class ArrayHelper
{
public void print2DArray(int[][]array)
{
  for(int i=0; i<array.length; i++)
  {
   for(int j=0; j<array[i].length;j++)
   {
    System.out.print(array[i][j]+" , ");
    
    
   }
   System.out.println();
  }
}

public int arraySum(int[]array)
{
  int sum=0;
  for(int i=0; i<array.length; i++)
  {
   sum=sum+array[i];
  }
   return sum;
}

public double arrayAvg(int[ ] array)
{
  double avg=0;
  for(int i=0; i<array.length; i++)
  {
   avg=(double)arraySum(array)/array.length;
  }
  return avg;
}

public int arraySum2D(int[ ][ ] array)
{
  int sum2=0;
  for(int i=0; i<array.length; i++)
  {
   sum2=sum2+arraySum(array[i]);
  }

  return sum2;
}

public double arrayAvg2D(int[ ][ ] array)
{
  double avg2=0;
  avg2=(double)arraySum2D(array)/(array.length * array[0].length);
  return avg2;
}



public int arrayMax2D(int[ ][ ] array)
{
  int max=0;
  for(int i=0; i<array.length; i++)
  {
   for(int j=0;j<array[i].length; j++)
   {
    if (array[i][j]>max)
    {
     max=array[i][j];
    }
   }
  }
  return max;
  
}

}

Explanation / Answer


public void print2DArray(int[][]array)
{
for(int i=0; i<array.length; i++)
{
   for(int j=0; j<array[i].length;j++)
   {
    if(j!=array[i].length-1)
       System.out.print(array[i][j]+" , ");
    else
       System.out.print(array[i][j]);

  
  
   }
   System.out.println();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote