Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple coding problem, Tabula Recta

Options
  • 22-03-2010 7:18pm
    #1
    Closed Accounts Posts: 16


    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 the craic 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 where i went wrong do the same!

    Thanks for any feedback... Code below;

    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]=s.charAt(index);
    index++;
    }
    index=0;
    for(int i=1;i<x;i++){
    array[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[j]=s.charAt(index);
    index++;
    }
    int index2=0;
    for(int j=column;j<x;j++){
    array[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[j]+" ");
    }
    System.out.println();
    }
    }

    }


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Moved from Design.


  • Registered Users Posts: 1,038 ✭✭✭rob1891


    public class Main {
        public static void main(String[] args) {
            char array[][];
            array=createTable(27,27,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            printTable(array,27);
        }
        private static char[][] createTable(int x, int y, String s) {
            char array[][]=new char[x][y];
            int index=0;
            array[0][0]=' '; 
            printTable(array,x);
            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++;
            }
    
            int column=0;
            for(int i=1;i<x;i++){
                for(int j=1; j<x; j++) {
                    array[i][j]=s.charAt((j-1+column)%(x-1));
                }
                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();
            }
        }
    
    } 
    

    First thing ... comments and sensible variable names and code. For instance, why are you passing x,y to createTable when you only ever do anything in terms of x and in any case, everything can be inferred form the sizeof the string ... never mind!

    I couldn't really work out what you were trying to do, though obviously you tried to print from the the middle of the string array to the end, and then from the start to somewhere in the middle.

    I've not made any attempt to fix that in the snippet above as I prefer the modulo operator. "%". It is the remainder from a division and very useful when trying to wrap around from the end of the array to the front as you iterate.

    In the line:

    array[j]=s.charAt((j-1+column)%(x-1));

    j-1 is iterating from 0->25, i.e. the character indexes of the string. Column is your start position in character string, and the %26 will do all the wrapping for you, so when the start position (column) is somewhere in the middle of the string and j-1+column gets to 26, beyond the last character in the string, 26%26 wraps it back around to the 0-th character, and at 27, 27%26 wraps it to the 1-th char, etc etc.

    Sorry if that is not clear, print out all the variables inside the iteration and it becomes obvious :-)

    Rob


  • Closed Accounts Posts: 16 FireHydrant


    Thank you very much for that, have used the modulo before, but only for simple math... Your explanation of the iteration did the job, and i understand it now, however still dont know why my code didn't work! Argh!

    I was error checking with the printing mid loop, and my commenting skills have always been lacklustre due to self taught programming!

    Thanks again!


Advertisement