-1

I try to print the matrix AxA with all of the values are showed in there, but I failed.

Output should be something look like this:

1 2 3
4 5 6

Thank you

So this is my code:

import java.util.Scanner;
public class Lab2 {
public static void main(String[] args) 
{ 
 Scanner input = new Scanner(System.in);

 System.out.print("Enter a number for rows: ");
    int rows = input.nextInt();

    System.out.print("Enter a number for columns: ");
    int columns = input.nextInt();

    int[][] array = new int[rows][columns];

    System.out.println("Enter the numbers in array: ");
    for (int i = 0; i < array.length; i++) 
    {
        for (int j = 0; j < array[i].length; j++)
            array[i][j] = input.nextInt();
    }
    System.out.println(" " + array);


  }
}

Updated: I was able to print out the matrix, but now I run into the second question is calculating the average of each rows and columns of the matrix. I tried to use this code, but it didn't work, and I know that I have a major problem right here, but I couldn't find one. I have been doing it since this morning until now. Not going to lie, this is my homework which to test my basic knowledge.

My new output should be:

     1, 2, 3, ave=2 
     4, 5, 6, ave=5
 ave=2.5, 3.5, 4.5

And this is my current code right now:

import java.util.Scanner;
public class Lab2 {
  public static void main(String[] args) 
  { 
    Scanner input = new Scanner(System.in);

        System.out.print("Enter a number for rows: ");
        int rows = input.nextInt();

        System.out.print("Enter a number for columns: ");
        int columns = input.nextInt();

        int[][] array = new int[rows][columns];

        System.out.println("Enter the numbers in array: ");

        for(int i=0 ; i<rows ; i++)
        {
          for(int j=0 ; j<columns ; j++)
          {
            array[i][j] = input.nextInt();
          }
        }
        for(int i=0 ; i<rows ; i++)
        {
           for(int j=0  ; j<columns ; j++)
          {
             System.out.print(array[i][j] + " , ");
          }
        System.out.println("\n");    
}  
}
  public static doube averageRow(int[][] array) {
    int rowTotal = 0;
    double average = 0;

    for (int rows = 0; rows < array.length; rows++) {
      for (int columns = 0; columns < array[rows].length; columns++) {
        rowTotal += array[rows][columns];
      }
      average = rowTotal / array[rows].length;
      System.out.println(average);
      rowTotal = 0;
    }
    return rowTotal;    
}
  public static doube averageColumn(int[][] array) {
    int columnTotal = 0;
    double average = 0;

    for (int columns = 0; columns < array.length; columns++) {
      for (int rows = 0; rows < array[columns].length; rows++) {
        columnTotal += array[rows][columns];
      }
      average = columnTotal / array[columns].length;
      System.out.println(average);
      columnTotal = 0;
    }
    return columnTotal;  
  }
}
TanoooooNg
  • 11
  • 1
  • 4
  • 1
    http://stackoverflow.com/search?q=%5Bjava%5D+print+array Please use the search before asking such questions. – Flikk Feb 17 '16 at 06:03

4 Answers4

1

You can use same logic for reading and writing array elements,Try this code

Reading an array...

for(int i=0;i<rows;i++)
{
    for(int j=0;j<columns;j++)
    {
       array[i][j] = input.nextInt();
    }
}

Printing array elements...

   for(int i=0;i<rows;i++)
    {
        for(int j=0;j<columns;j++)
        {
          System.out.print(array[i][j] + "  ");
        }
        System.out.println("\n");
    }
Mohith P
  • 585
  • 5
  • 14
1

Use

Stream.of(array).map(Arrays::toString).forEach(System.out::println);

Which will print :

[1 ,2, 3]

[4, 5, 6]

or

System.out.println(Arrays.deepToString(array));

Reference

Community
  • 1
  • 1
Arun Xavier
  • 763
  • 8
  • 47
0

Keep in mind that you can't simply print the whole array. I.e.: System.out.println(" " + array);

You're on the right track but you need to print using a for loop (give printf a try, it's powerful):

for (int i = 0; i < array.length; i++) 
    {
        for (int j = 0; j < array[i].length; j++){
            System.out.printf("%d ", array[i][j]);
        }
        System.out.printf("\n");
    }
Dan L
  • 396
  • 3
  • 13
0

There is another single loop way I can think of:

  for (int[] a : array) {
    String str = Arrays.toString(a);
    System.out.println(str.substring(1, str.length()-1).replaceAll("[,]", ""));
  }

The for loop basically iterates over each row, i.e. value of 'a' is an array (row) in the matrix. Arrays.toString(a) returns the string equivalent of the array, e.g. for a = [1,2,3,4,5], it would return "[1, 2, 3, 4, 5]". Now simply remove the brackets (substring) and replace commas (",") with "", and you have the desired result. Note that the first parameter of the replaceAll is a regex, not the string itself.

karzler007
  • 111
  • 9