Hello..

I am working on this project which is to use a matrix to encrypt a string& a text file and to decrypt them as well.

The matrix that I need to use is this one, but I still don't know how to make it shift each character to the right by one position each time it creates a new row.

public class Matrix
{
	private final int SIZE = 45;
	private char[ ] [ ] matrix;
	private String alpha;

	public Matrix()
	{
		alpha = createString();
		matrix = new char[SIZE][SIZE];
		fillMatrix();
	}//end const
	private  String createString()
	{
		StringBuilder sb = new StringBuilder();
		//make the string
		for(char c = 'A'; c <= 'Z'; c++)
			sb.append(c);
		sb.append(" .!;:,$%#@=&*()+=<>");
		return sb.toString();
	}//end create
	private void fillMatrix()//fill matrix according to plan
	{
		for(int row = 0; row < SIZE; row++)
		{
			for(int col = 0; col < SIZE; col++)
			{
				matrix[row][col]= alpha.charAt(col);
				System.out.print(matrix[row][col]);
			}//end for
			//shift
			//add chars for front of alpha to back of matrix row
			System.out.println();
		}//end for

	}//end build

	public String toString()
	{
		return "toString answer goes back: ";
	}//end twoString
	public static void main(String[ ]  args)
	{
		Matrix m = new Matrix();
		System.out.println(m.createString());
		//System.out.println();
	}//end main
}//end class

It should look like this:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZ A
CDEFGHIJKLMNOPQRSTUVWXYZ AB
DEFGHIJKLMNOPQRSTUVWXYZ ABC
EFGHIJKLMNOPQRSTUVWXYZ ABCD
FGHIJKLMNOPQRSTUVWXYZ ABCDE
GHIJKLMNOPQRSTUVWXYZ ABCDEF
HIJKLMNOPQRSTUVWXYZ ABCDEFG
IJKLMNOPQRSTUVWXYZ ABCDEFGH
JKLMNOPQRSTUVWXYZ ABCDEFGHI
KLMNOPQRSTUVWXYZ ABCDEFGHIJ
LMNOPQRSTUVWXYZ ABCDEFGHIJK
MNOPQRSTUVWXYZ ABCDEFGHIJKL
NOPQRSTUVWXYZ ABCDEFGHIJKLM
OPQRSTUVWXYZ ABCDEFGHIJKLMN
PQRSTUVWXYZ ABCDEFGHIJKLMNO
QRSTUVWXYZ ABCDEFGHIJKLMNOP
RSTUVWXYZ ABCDEFGHIJKLMNOPQ
STUVWXYZ ABCDEFGHIJKLMNOPQR
TUVWXYZ ABCDEFGHIJKLMNOPQRS
UVWXYZ ABCDEFGHIJKLMNOPQRST
VWXYZ ABCDEFGHIJKLMNOPQRSTU
WXYZ ABCDEFGHIJKLMNOPQRSTUV
XYZ ABCDEFGHIJKLMNOPQRSTUVW
YZ ABCDEFGHIJKLMNOPQRSTUVWX
Z ABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ

except it should include characters too.

Recommended Answers

All 9 Replies

If you want to assign a value to each of your array while you are going through a loop, it would be easier to look at when you move the row loop to be inside the col loop. Also, you need to control the column position where you want to put the value in.

for (int col=0; col<SIZE; col++) {
  char assgChar = alpha.charAt(col);
  int current_col = col;
  for (int row=0; row<SIZE; row++) {
    matrix[row][current_col]= assgChar;
    current_col = (current_col==0) ? (SIZE-1) : (current_col-1); // imitate shifting
  }
}

By the way, you have a matrix size of 45x45 but you have only 27 string length?

There's a sneaky way to do this...
append a copy of your characterset string to it self - ie have a string that has the letters twice "ABC ... XYZABC ... XYZ"
now you can simply take a 26-long substring starting at 0, 1, 2, ... 25 to get the result you showed or index individual chars as in
matrix[row][col]= alpha.charAt(row+ col);

(for "A-Z" and "26" substitute you own character set, of course)

I've modified my fillMatrix method, but I'm still not getting the shifting!

private void fillMatrix()//fill matrix according to plan
		{
			int col, row, pos;
			for( row = 0; row < matrix.length; row++)
			{
				pos=0;
				for(  col = 0; col < matrix[0].length-row; col++)
				{
					matrix[row][col]= alpha.charAt(row+col);
				}//end for
				while(col<matrix[0].length)
				{
					matrix[row][col++]= alpha.charAt(pos++);
				} // end while

			}//end for

	}//end fill

I don't understand the "-row" in line 7.
You load the matrix in lines 7-10 then immediately overwrite part of it with lines11-14. Why?

You have the right code in there - just remove the wrong code!

for (row = 0; row < matrix.length; row++)  {
  for (col = 0; col < matrix[row].length; col++) {
    matrix[row][col]= alpha.charAt(row+col);
  }
}

This is the code with comments ..

private void buildMatrix( )
{
int row, pos, col;
//fill the rows up to character > (last char) of string alpha
for( row = 0; row < matrix.length; row++ )
{
pos = 0;//keep track of character index in the string, resets for each row
//this loop stops adding chars “row” positions from the end
for( col = 0; col < matrix[ 0 ].length - row; col++ )
{
matrix[ row ][ col ] = alpha.charAt( col + row );
}//end for
//add “row” characters of alpha starting with position 0 to finish filling the row
while(col < matrix[0].length)
{
matrix[row][col++] = alpha.charAt(pos++);
}//end while
}//end for
}//end buildMatrix

buildMatrix() is just fillMatrix()

I don't know what change I should make to make the shifting as in the picture above ..
In what method should the change be done?

Please re-read my previous posts - I don't know how to make this any clearer!
(don't forget to double up your "alpha" string alpha= alpha+alpha; as I explained earlier)

It looks like you are trying to fill (for example line 3) by putting in D-Z then having a second loop to put A-C at the end, yes? With a bit of debugging you can do it that way, but the way I suggested is quicker and easier to program.

Thanks.
I tried a different approach.
That's what I'm doing now .. it seems to be working :)

private void fillMatrix()
	{
		int col, row, pos;
		for( row = 0; row < matrix.length; row++)
		{
			pos=0;
			for( col = 0; col < 45-row; col++)
			{
				matrix[row][col]= alpha.charAt(row+col);
			}
			while(col<matrix[0].length)
			{
				matrix[row][col++]= alpha.charAt(pos++);
			}
		}
		for( row = 0; row <= SIZE-1; row++)
		{
			for( col = 0; col < SIZE-1; col++)
			{
				System.out.print(matrix[row][col]);
			}
			System.out.println();
		}
	}

it seems to be working :)

That's great! In the end it doesn't matter exactly what algorithm you use, as long as the code is understandable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.