1

In my class we have to make a matrix filler program but I have gotten very confused on how to do so by using the user input and I don't know how to at all. Iv'e tried to start coding but can't get past step 1.

package question4;

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

public class MatrixFiller {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Size of Matrix: ");
        Random ranGen = new Random();
        int matrixSize = input.nextInt();
        int a = matrixSize * matrixSize;
        input.close();
        int[][] myMatrix = new int[matrixSize][matrixSize];
        for (int x = 0; x < matrixSize; x++) {
            for (int y = 0; y < matrixSize; y++) {
                myMatrix[x][y] = ranGen.nextInt(a);
                System.out.print(Integer.toString(myMatrix[x][y]) + " ");
            }
            System.out.print("\n");
        }
    }
}

so i fixed the code and was wandering how can i add the zero inferno of the number like 1 and 2 so it comes out 01 and 02, do i need an if loop so that it only checks numbers less then 10?

Phantom1421
  • 29
  • 1
  • 9
  • Here's a start: You make a 2 dimensonal array (i.e. a matrix): ```int [][] arr = new int[x][x]```, then you start filling in the arrray as you take in the numbers from the user via a nested loop. E.g. the ```08``` on line 3 would go to ```arr[2][3]``` position of the array (following row-major format) – Siddhartha Oct 18 '15 at 00:57
  • if its random can i just import the random for java datatype cause the user is only suppose to choose the size? – Phantom1421 Oct 18 '15 at 00:58
  • "*random elements from 1 to x2.*" What is x2? The user enters it? – user1803551 Oct 18 '15 at 01:00
  • Yes, java ```Math``` library has a ```Random```, which you can use to [generate random numbers](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java) between 1 and x^2. – Siddhartha Oct 18 '15 at 01:01
  • @user1803551 I think he means x^2. – Siddhartha Oct 18 '15 at 01:01
  • i add stuff to get you and idea if my code is on right track – Phantom1421 Oct 18 '15 at 01:06
  • Your array needs to be x by x, why do you have different values for x and y? Also, did you learn about arrays? How to define one and how to access it? – user1803551 Oct 18 '15 at 01:08
  • @Siddhartha Huh, makes sense. – user1803551 Oct 18 '15 at 01:09
  • I didn't , I missed my class discussing it and I'm having a hard time trying to understand it , which is part why I made this post looking like a fool – Phantom1421 Oct 18 '15 at 01:10
  • @Phantom1421 your nested loops are on the right track, here's some more hints. First, ```scores = [x] [y];``` makes no sense, the ```[x][y]``` needs to belong to a matrix such as ```arr``` that I mentioned above. Second, you're designing a ```square matrix```, so your loops will both run the same number of iterations. Look up java 2D matrices. – Siddhartha Oct 18 '15 at 01:14
  • Where is your code for reading user input? – Bon Oct 18 '15 at 01:34

1 Answers1

1

By your example code it seems that what you are missing is basic syntax knowledge. Let me refresh your memory on arrays at the most basic level with simple language.

Arrays are like a multi-dimensional list of variables of some type.

  • You choose the type of variables when you declare the array.
  • The amount of variables which an array can hold is a constant number (the length of the array) which is defined when is is initialized.
  • An array can also have more than one dimensions. You set the number of dimensions when you declare the array. Think of a 1 dimensional array as a list, 2 dimensions would turn the list into a matrix. In this case, you need to set the length of each dimension (when you initialize). So, if the length of the 2 dimensions is the same you get a square, otherwise you get a rectangle.

Here is some code to go along with this:

int[] myArray;

Here I declared a 1 dimensional array which holds ints.

myArray = new int[6];

Here I initialized my array and set the length of the dimension to 6.

int[] myArray2 = new int[7];

I can also do them on the same line.

long[][] myMatrix = new long[3][2];

Here I declared a 2 dimensional array which holds longs. The lengths of the dimensions are 3 and 2, so it looks like this when you imagine it:

_ _
_ _
_ _

Now we wan to access the array at a certain position. This is done by specifying the array name and the position in each dimension you want to access, like this:

myMatrix[0][1] = 63;

Remember! The position start counting from 0, so a 2 by 3 array would have the first dimension values 0 and 1; and the second dimension values 0, 1 and 2.

Now let's iterate over an array and put the number 6 in all of its slots:

int[][] exmaple = new int[3][3]; // 2 dim. array of ints with size 3 by 3.
for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
         example[x][y] = 6;
    }
}

Not sure if you need this, but I will mention a few additional notes:

  • You can initialize an array with values directly and then you don't need to specify the dimensions' lengths:

    int[][] array = new int[][] {{1 ,2}, {5, 65}}; // 2 by 2
    
  • You can get the length of a dimension of an array by the syntax

    array.length;    
    array[0].length;
    array[1].length;
    // etc.
    

    These return an int which you can use as a bound when looping:

    for (int i = 0; i < array.length; i++) {
       // ...
    }
    
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • i edited my code with more things, is it making more sense, and i still don't fully understand how to get an array working??? – Phantom1421 Oct 18 '15 at 02:36
  • @Phantom1421 I don't see you using anything I explained. – user1803551 Oct 18 '15 at 02:40
  • Start by fixing your syntax errors using what I explained. – user1803551 Oct 18 '15 at 02:42
  • your syntax wants me to set the values before, but i need to have them userdefined? – Phantom1421 Oct 18 '15 at 02:44
  • Set the values before what? You read from the user the length of the dimensions, use that number to *initialize* the array. – user1803551 Oct 18 '15 at 02:46
  • Yes! Now, use the syntax for iterating and putting values into the array. Also, what are you declaring `int x` and `int myMatrix` for? Remember that `x` and `y` need to be `int`s in the loops. – user1803551 Oct 18 '15 at 02:54
  • well see i need to implement the random datatype which i imported but i don't know how to set the matrix for it? – Phantom1421 Oct 18 '15 at 02:56
  • The datatype of the array is `int`. What you need to do is generate a random int, but before you do that you have to fix your syntax. – user1803551 Oct 18 '15 at 03:00
  • i think i fixed my syntax?? – Phantom1421 Oct 18 '15 at 03:04
  • when i run the code it prints these weird numbers, [[I@4e25154f – Phantom1421 Oct 18 '15 at 03:09
  • You fixed the syntax. But `int[][] myMatrix = new int[ranInt][ranInt];` makes the matrix of random size instead of using the size given by the user. And `myMatrix [x][y]= matrixSize;` puts the value of the size of the matrix in all of its cells. About the print, you need to iterate over the array and print each value, you can't print an array variable. – user1803551 Oct 18 '15 at 03:12
  • ok edited it but when i print i get one long line of matrix with the , Size of Matrix: 5 [[6, 6, 6, 6, 6], [6, 6, 6, 6, 6], [6, 6, 6, 6, 6], [6, 6, 6, 6, 6], [6, 6, 6, 6, 6]] – Phantom1421 Oct 18 '15 at 03:22
  • You get the same value because you generate a value and save it in `int ranInt= ranGen.nextInt(9);`. Then you put that value in all the cells with `myMatrix [x][y]= ranInt;`. Instead, you want to generate a new value for each cell. Also, why do you generate a number between 1 and 9? – user1803551 Oct 18 '15 at 03:26
  • so firs too i run the ranInt in an if loop or someplace, and also it part of the homework where he says the a number 0-9 – Phantom1421 Oct 18 '15 at 03:28
  • `ranGen.nextInt(9);` gives you a new random int. Just put it directly into your array cell. Also, in your example there are numbers greater than 9, so I was confused. Whichever number it is, you're doing it correctly. – user1803551 Oct 18 '15 at 03:31
  • i think i got but how do i put staking on top of each other ? – Phantom1421 Oct 18 '15 at 03:33
  • Now that you are not using `ranInt` you can remove it. What stacking? You want to print the array like a matrix? Maybe [this](http://stackoverflow.com/questions/5061912/printing-out-a-2-d-array-in-matrix-format)? – user1803551 Oct 18 '15 at 03:45
  • dont know why but it doesn't work???, i edited my code and it does work for some odd reason – Phantom1421 Oct 18 '15 at 03:48
  • @Phantom1421 Do a separate loop for putting the values and a separate one for printing, as shown in the example. – user1803551 Oct 18 '15 at 12:24
  • So I need to add an extra loop for just the printing? – Phantom1421 Oct 18 '15 at 13:57
  • @Phantom1421 You don't have to, you can print as you iterate through the first loops, I just think you'll understand better if you do them separately. – user1803551 Oct 18 '15 at 14:11
  • @Phantom1421 I pasted a link in the previous comments that shows you how to do it. You really need just to copy-paste from there. – user1803551 Oct 18 '15 at 21:03
  • [Search the site](http://stackoverflow.com/questions/11851403/save-an-integer-in-two-digit-format-in-a-variable-in-java). – user1803551 Oct 18 '15 at 21:09
  • i changed my code and added some things if you want to see and i have one more thing that it will be finished, and also i really and very thankful for your help @user1803551 – Phantom1421 Oct 18 '15 at 22:43
  • it can't format object into a given number is what get as an error? – Phantom1421 Oct 19 '15 at 01:11
  • Your sentence makes no sense. Copy paste the answer in the link. – user1803551 Oct 19 '15 at 20:12