How do I output location of elements in a 2d array?

java

#1

I'm trying to modify my countinstance method so that it returns the row and col location of every number that is evenly divisible by 3, so that I can print a -1(indicating that the corresponding number is divisible by 3) in the same location in another grid(of the same size) that is filled with the -1 indicators and the numbers that aren't divisible by 3.

The main thing I'm struggling with is how to return the locations and then print them out. I'm also struggling with creating a second method/array that prints out all of the numbers(the -1s and the other numbers) in the same locations.

2d arrays and methods have been my worst topics in java so far, so any help would really be appreciated.

Here is my code, so far:

     public class practice 
{

   public int[][] createArray(int rSize, int cSize) {
        Random r = new Random();
        int[][] array = new int[rSize][cSize];
        for (int row = 0; row < array.length; row++) {
            for (int col = 0; col < array[0].length; col++) {
                array[row][col] = r.nextInt(25);
            }
        }
        return array;
    }
    public void print2DArray(int[][] Array) {
        for (int row = 0; row < Array.length; row++) {
            for (int col = 0; col < Array[0].length; col++) {
                System.out.print(Array[row][col] + "\t");
            }
            System.out.println("\n");
        }
    }
    public int[][] createCoords(int[][] Array) {
        int[][] coord = {
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1} 
        };
        for (int row = 0; row < coord.length; row++) {
            for (int col = 0; col < coord[0].length; col++) {
                System.out.print(coord[row][col] + "\t");
            }
            System.out.println("\n");
        }
        for (int row = 0; row < coord.length; row++) {
            for (int col = 0; col < Array[row].length; col++) {
                if (Array[row][col] % 3 == 0) coord[row][col] = -1;
            }
        }
        return coord;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System. in );
        practice c = new practice();
        int[][] myArray;
        
        myArray = c.createArray(10, 10);
        
        c.print2DArray(myArray);
        c.createCoords(myArray);
    }
}

#2

Is this related to a console session in Codecademy or are you using an IDE ?

Updated
I'm curious whether you've placed all your code in the window or dropped a class declaration.
Where have you declared your updated Console class ?

If anyone else reads this and can give me an answer, please do.


#3

Yeah, I just left out my public class, sorry. I'm using netbeans.


#4

Ok, can you elaborate on this ?
Where is the extended Console class that you used for

Or if I'm out of my depth here, how did you derive the

section of code ?


#5

I updated my code to include the console


#6

Ok, got it. That's what I thought was missing. I'm going to separate the
public static void
into an application project file
and use Console as a class of it.
Will get back to you once I have an answer. Nice problem to solve.


#7

Hey TM, I got the code running after a bit of stuffing around with misinterpreting java built in Console code ! LOL. Good learning experience.
So I'm getting a print out of the main matrix and entered a search number and got something.
While I'm fiddling around with this, what was the specific problem/s you're having ?
Give me some examples of output expected and what was wrong.


#9

Lol. I updated the code above to reflect where I am at. I have most of it figured out but I'm stuck on the line: Arraycopy = c.createCoords([10][10]); no matter what I do I cant get it to work properly. It keeps giving me different errors


#10

Is this how your lecturer wants you to code the assignment ? Because otherwise, why go to the trouble of all that unnecessary code when you can simply use a flat array, fill it, do not use Console, simply use println for a 10x10 , do your searching and conversion on the flat array and then do the next println on the array. Heck of a lot easier.
Unless he insists on doing it as an intellectual exercise ???