-1

I'm trying to make a program that will make square matrices based on user input. I know that arrays exist, but I wanted to make a matrix from scratch so that I could better understand the basic concept of it and further extend my understanding of loops. So far I have been able to make a square matrix that will accept one number as an input into that matrix, for example I input a square 2x2 matrix and while I want it to look like this 1 2 3 4 with 1 and 2 being above 3 and 4. I have only gotten it to accept one user input that it places in all four slots. For example, if my user input is 1 then the matrix looks like this 1 1 1 1

My code looks like this thus far:

    int number;
    System.out.println("What are the dimensions of the matrix?");
    number = in.nextInt();

    for (int k = 0; k < number; k = k +1)
    {
        System.out.println("What are the numbers in your matrix?");
        int matrix_number = in.nextInt();

        for (int i = 0; i < number; i = i + 1)
        {
            for (int j = 0; j < number; j = j + 1)
            {
                 System.out.print(matrix_number);
            }

            System.out.println();

        }

    }

I believe that my problem lies in my first for loop where I have the user input the matrix number. Any helpful suggestions on how I can better write this so that the user can input a different number for each slot in the matrix?

M. B.
  • 1
  • 1
  • 1

4 Answers4

1

It looks like you are trying to create a matrix and then populate it with values read from the user.

To create an N x N matrix of integers

    int[][] matrix = new int[n][n]();

To assign a value to a matrix cell [i, j]:

    matrix[i][j] = someValue;

Obviously, if you want to read a different value for each cell, you need to call nextInt() multiple times; i.e. once for each value you want to read.

(Note to other readers: I'm not coding this for the OP, because he will learn more by coding it himself.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

You can create a matrix using 2 dimensional arrays:

int[][] matrix = new int[row][column]; //row is the number of matrix rows
                                       //column is the number of matrix columns

To access the elements of the matrix and define it after the declaration, you can use a nested for loop:

for (i = 0; i < row; i++ )
     for (j = 0; j < column; j++)
     {
        scores[i][j] = value; // value is your chosen integer for that index
     }
}
Mestica
  • 1,489
  • 4
  • 23
  • 33
0

As you mention in your question, user has to input only onces and that it places in all four slots. For example, if user input is 1 then the matrix looks like this 1 1 1 1. Then no need for first for loop, just remove it.

    int number;
    System.out.println("What are the dimensions of the matrix?");
    number = in.nextInt();

    System.out.println("What are the numbers in your matrix?");
    int matrix_number = in.nextInt();

    for (int i = 0; i < number; i = i + 1)
    {
       for (int j = 0; j < number; j = j + 1)
       {
            System.out.print(matrix_number);
       }

       System.out.println();
   }
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
0

You want the user to say the size of the square matrix, then you want the user to tell you every number in the matrix. You only need two loops here:

int number;
System.out.println("What are the dimensions of the matrix?");
number = in.nextInt();

for (int i = 0; i < number; i = i + 1)
{
    for (int j = 0; j < number; j = j + 1)
    {
        System.out.println("What are the numbers in your matrix?");
        int matrix_number = in.nextInt();
        System.out.print(matrix_number);
    }
    System.out.println();
}

If you don't want your matrix polluted by "What are the numbers in your matrix?" questions, then you're going to need to learn how to store user input into some sort of data structure. As you said in your question, arrays are a great way to do this (as are 2d arrays).

If you were willing to learn file input or file output, then you could do what you seek without "storing" the numbers in an array. Either read the numbers in from a file and output them to the screen, or have the user type them as user input and output the matrix to a file.

Edit: You could try to erase the "What are the numbers in your matrix?" system out by printing backspace characters on linux systems. More here: How to delete stuff printed to console by System.out.println()?

Community
  • 1
  • 1