Hey I'm trying to create a simple Vigenere cipher table like this in Java...
http://cairnarvon.rotahall.org/pics/tabularecta.png
I'm doing it for fun but have literally been staring at this code for days now without any brainwaves! If this isn't the right forum, let me know, if you can figure out why the string reaches the boundary do the same!

Thanks for any feedback... Code attached;

public class Main {
public static void main(String[] args) {
char array[][];
array=createTable(27,27,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

}
private static char[][] createTable(int x, int y, String s) {
char array[][]=new char[x][y];
int index=0;
array[0][0]=' ';
for(int i=1;i<x;i++){
array[0][i]=s.charAt(index);
index++;
}
index=0;
for(int i=1;i<x;i++){
array[i][0]=s.charAt(index);
index++;
}
index=0;
int column=1;
for(int i=1;i<x;i++){
index=i-1;
for(int j=1;j<column;j++){
array[i][j]=s.charAt(index);
index++;
}
int index2=0;
for(int j=column;j<x;j++){
array[i][j]=s.charAt(index2);
index2++;
}
column++;
}
return array;
}

private static void printTable(char[][] array,int x) {
System.out.println();
for(int i=0;i<x;i++){
for(int j=0;j<x;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}

}

Edited 6 Years Ago by peter_budo: Please do not use CODE SNIPPED for posting question. Use FORUM THREAD.

An array of chars is not a string.
Here you can read more about arrays:
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

Good luck :)

You should look at code before replying s.charAt(index) @honohanf good start but you got little lost in looping, also few things can be simplified

public class Main {
	public static void main(String[] args) {
		char[] alphabet = {'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'};
		System.out.println(alphabet.length);
		char[][] array =createTable(alphabet);
		printTable(array);
	}
	
	private static char[][] createTable(char[] alphabet) {
		char[][] table = new char[alphabet.length][alphabet.length];
		int a;
		for(int i = 0; i < alphabet.length; i++){
			for(int j = 0; j < alphabet.length; j++){
				a = i+j;
				if(a >= alphabet.length){
					a = a - alphabet.length;
				}
				table[i][j] = alphabet[a];
			}
		}
		return table;
	}
	
	private static void printTable(char[][] table) {
		for(int i = 0; i < table.length; i++){
			for(int j = 0; j < table.length; j++){
				System.out.print(table[i][j]);
			}
			System.out.println();
		}
	}

}
char array[][];
array=createTable(27,27,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
char[] alphabet = {'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'};

@peter_budo
People write code in different ways but you have obviously chosen (as I would) to use chars instead of a string when you use an array of chars.
Maybe my answer in the earlier post was to short and for that I apologize.

This article has been dead for over six months. Start a new discussion instead.