convert string from args[0] into char then populat

2019-09-10 14:58发布

问题:

public static void main(String[] args) {

    String input = args[0]; 

    String [] part = input.split(" "); 
    //splits string into 2 parts (action and characters to encode)
    String action = part[0];
    // action is what is done to letter i.e. decrypt or encrypt
    String plainText = part[0];
    char [] letters = plainText.toCharArray();
    //letters is the input of what to act on

        int n = plainText.length();
        //number of letters typed
        int boxsize; // overall size of box  
        boxsize = (int) Math.pow((Math.sqrt(n))+1,2);
        int Row = (int) Math.sqrt(boxsize); 
        int Col = Row;
        //length of rows AND columns since its square

    if (action.equals("-encrypt")) {

        char [][] box = new char[Row][Col];

        for (int i=0; i<Row; i++) {
        for ( int j=0; j<Col; j++) {

            System.out.println( i );

how do you populate a 2d char array if the argument is given as a string? also how do you print the box (array) so that it reads vertically down each column left to right?\ an eg of command line would be "-encrypt abcd" (exclude quotation marks) output i want is "acbd"

回答1:

String plainText = part[1];

Was copied with 0

But generally without quotation marks args[0] would be the action, the rest args[>=1] spaced plainText.