missrigby
missrigby

Reputation: 11

Filling a 2D array in Java

I am new to java and I am struggling immensely! I've written the following code but keep getting errors. All I am trying to do at the moment is fill a 5x5 matrix with the letter A. Here's what I have so far, I am not sure if I need to post the errors as well? Any help would be really greatly appreciated.

public class Encryption {   
    private String Unencoded, FiveLetterKeyword, EncryptedMessage;

    //constructor method
    public Encryption(String U, String F, String E)
    {
        Unencoded = U;
        FiveLetterKeyword = F;
        EncryptedMessage = E;

    }
    //create 2D string array 5 by 5
    String Encrypt [][] = new String[5][5];        

    //create string filled with all letters of the alphabet
    String String = new String
        ("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");



    //method for loop to print out the matrix
    public static void matrix()
    //for loop to create matrix rows and columns        
    {
        for (int row = 1; row < Encrypt.length; row++)
        {
            for (int column = 1; column < Encrypt[row].length; column++)

            System.out.print(Encrypt[row][column] + " ");
        }       
    }

    //filling the array with the letter A
    public char Encrypt(char Encrypt[][])
    {
        //char[] alpha = alphabets.toCharArray;

        //declaring variable to fill array with A           
        char aChar = "A";

        for (int row = 1; row < Encrypt.length; row++)
        {
            for (int column = 1; column < Encrypt.length; column++)

            return Encrypt;
        }   
    }   
}

Upvotes: 1

Views: 22715

Answers (2)

DaoWen
DaoWen

Reputation: 33019

Arrays in Java are zero-based, which means they start at index zero, and range until index array.length-1.

Your code starts the row and column at 1—which means you're skipping the initialization of row/column 0. That's probably where at least some of the problems are coming from, since you're using your 5x5 array (rows/columns 0,1,2,3,4) as a 4x4 array (rows/columns 1,2,3,4).

There's also the fact that your Encrypt method doesn't actually make any assignments to the array. You probably want to initialize it like this:

// NOTE: changed return type to void -- this is a side-effect-only method!
public void Encrypt(char Encrypt[][])
{
    // NOTE: use single-quotes for chars. double-quotes are for strings.
    char aChar = 'A';
    // NOTE: changed the starting loop values from `1` to `0`
    for (int row = 0; row < Encrypt.length; row++)
    {
        // NOTE: technically Encrypt.length works here since it's a square
        // 2D array, but you should really loop until Encrypt[row].length
        for (int column = 0; column < Encrypt[row].length; column++)
        {
            // NOTE: set each entry to the desired char value
            Encrypt[row][column] = aChar;
        }
    }   
}

There are several issues with your original code. Look at the NOTE entries in the comments for individual explanations.

Upvotes: 4

hofan41
hofan41

Reputation: 1468

You are missing the most crucial part of what you are trying to accomplish.

Where are you setting your matrix to the letter A?

Change your Encrypt function to the following:

//filling the array with the letter A
public void Encrypt(char arr[][])
{
    //char[] alpha = alphabets.toCharArray;

    //declaring variable to fill array with A           
    char aChar = 'A';

    for (int row = 0; row < arr.length; row++)
    {
        for (int column = 0; column < arr[row].length; column++)
        {
            arr[row][column] = aChar;
        }
    }   
}   

Upvotes: 2

Related Questions