Click to See Complete Forum and Search --> : Printing in rows, then columns


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");
}

}

dlorde
September 16th, 2009, 03:51 AM
This isn't so much a Java problem as an algorithm problem. Come up with an algorithm to do the job, test it out by hand until you are confident that it works, then translate it into Java. If you have trouble translating it into Java, we will try to help. Trying to develop an algorithm as you write the Java code is asking for trouble.

If you already have an algorithm worked out, post it up so we can compare it with the Java code.

If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
Anon.

jcaccia
September 16th, 2009, 08:16 PM
The loop that copies the keyword to the table works just because you tested it with a word of less than 5 letters. To guarantee it will work with keywords of any length (between 1 and 25, of course) it should be something like this:
for (int i = 0; i < keyword.length(); i++) {
int r = i / 5;
int c = i % 5;
table[r][c] = keyword.charAt(i);
}
Please notice you use j as an index for keyword and also as the column index for table. In the first case it can range from 0 (if the keyword has no letters) to 25 (if it is the full alphabet), while as column index for table it should range from 0 to 5 only.

Now the loop to copy the alphabet to the table. First, change the formulae to calculate row and column. For both you use keyword.length(), but that does not give the last position occupied by the keyword (this is what caused the output to be wrong). That position should be calculated using keyword.length() - 1, like this:
int l = keyword.length() - 1;
int row = (l / 5) + 1;
int column = (l % 5) + 1;
Now adjust column in case it is out of range (> 5, when the number of letters in keyword is a multiple of 5):
if (column > 5) {
column = 0;
row++;
}

And finally the loop should be:
for (int c = 0; c < 5; c++) {
if (c == column) { // use ==, not >=
row--;
column = -1; // to prevent this if from executing again
}
from (int r = row; r < 5; r++) {
table[r][c] = alphabet.charAt(position++);
}
}

blinksumgreen
September 17th, 2009, 01:48 PM
Thank you!