blinksumgreen
September 15th, 2009, 09:53 PM
I'm in the final stages of a project and I have a question.
Recape + New info - keyword has any non unique characters removed.
"Alphabet (without J, so as to keep it to 25 letters, and therefore a nice square)" has any characters that were in keyword removed from it.
Both of these are completed within other methods and it can be assumed that keyword will not have non unique characters, and that alphabet has already had the characters that are in keyword removed.
Fill in the table with the keyword by rows, and then fill in the rest of the table with the remaining alphabet, by columns. How do I do this? I've tried multidimensional arrays and using mod, but I can't quite figure it out.
Example:
Keyword: computer
alphabet: ABDFGHIKLNQSUVWXYZ
C O M P U
T E R N W
A F I Q X
B G K S Y
D H L V Z
The code below is what I've come up with, but the output it a little off. One big issue I'm having is that if you add a word more than 4 characters then there's an ArrayIndexOutOfBounds erro thrown... which makes me think I don't understand mulitdimensional arrays. I thought the code for adding the keyword to the array would work. Any thoughts?
An input I used was keyword: key
Output:
K E Y [] T
A F L P U
B G M Q V
C H N R W
D I O S X
The output should be:
K E Y P U
A F L Q V
B G M R W
C H N S X
D I O T Z
public void printKeyTable(String keyword, String alphabet) {
char[][] table = new char[5][5];
// determines how many rows the keyword is in and adds one (next row to print in)
int row = (keyword.length() / 5) + 1;
// determines what column the keyword ended in
int column = (keyword.length() % 5) + 1;
/*
* adds the keyword to the table
*/
for (int j = 0; j < keyword.length(); j++) {
int i = 0;
if(j!=0 && j%5==0){
i++;
}
table[i][j] = keyword.charAt(j);
}
/*
* adds the alphabet to the table
*/
int position = 0;
for (int j = 0; j < 5; j++) {
if(j >= column){
row = row-1;
}
for (int i = row; i < 5; i++) {
table[i][j] = alphabet.charAt(position);
position++;
}
}
/*
* prints the table
*/
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(table[i][j]);
}
System.out.println("\n");
}
}
Recape + New info - keyword has any non unique characters removed.
"Alphabet (without J, so as to keep it to 25 letters, and therefore a nice square)" has any characters that were in keyword removed from it.
Both of these are completed within other methods and it can be assumed that keyword will not have non unique characters, and that alphabet has already had the characters that are in keyword removed.
Fill in the table with the keyword by rows, and then fill in the rest of the table with the remaining alphabet, by columns. How do I do this? I've tried multidimensional arrays and using mod, but I can't quite figure it out.
Example:
Keyword: computer
alphabet: ABDFGHIKLNQSUVWXYZ
C O M P U
T E R N W
A F I Q X
B G K S Y
D H L V Z
The code below is what I've come up with, but the output it a little off. One big issue I'm having is that if you add a word more than 4 characters then there's an ArrayIndexOutOfBounds erro thrown... which makes me think I don't understand mulitdimensional arrays. I thought the code for adding the keyword to the array would work. Any thoughts?
An input I used was keyword: key
Output:
K E Y [] T
A F L P U
B G M Q V
C H N R W
D I O S X
The output should be:
K E Y P U
A F L Q V
B G M R W
C H N S X
D I O T Z
public void printKeyTable(String keyword, String alphabet) {
char[][] table = new char[5][5];
// determines how many rows the keyword is in and adds one (next row to print in)
int row = (keyword.length() / 5) + 1;
// determines what column the keyword ended in
int column = (keyword.length() % 5) + 1;
/*
* adds the keyword to the table
*/
for (int j = 0; j < keyword.length(); j++) {
int i = 0;
if(j!=0 && j%5==0){
i++;
}
table[i][j] = keyword.charAt(j);
}
/*
* adds the alphabet to the table
*/
int position = 0;
for (int j = 0; j < 5; j++) {
if(j >= column){
row = row-1;
}
for (int i = row; i < 5; i++) {
table[i][j] = alphabet.charAt(position);
position++;
}
}
/*
* prints the table
*/
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(table[i][j]);
}
System.out.println("\n");
}
}