Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从二维数组打印时删除字符_Java_Arrays - Fatal编程技术网

Java 从二维数组打印时删除字符

Java 从二维数组打印时删除字符,java,arrays,Java,Arrays,我有一个字符串,我想通过按行加载到2D数组中,然后按列打印数组来加密它。以便: |A | B| |C|D| |E| 加密到“ACEBD” 然而,我似乎无法避免在得到“ACBD”的输出中删除最后一行中的字符。你知道怎么解决这个问题吗 public static void main(String[] args) throws FileNotFoundException { if (handleArguments(args)) System.out.println("encr

我有一个字符串,我想通过按行加载到2D数组中,然后按列打印数组来加密它。以便:


|A | B| |C|D| |E| 加密到“ACEBD”

然而,我似乎无法避免在得到“ACBD”的输出中删除最后一行中的字符。你知道怎么解决这个问题吗

public static void main(String[] args) throws FileNotFoundException {
    if (handleArguments(args))
        System.out.println("encrypt");


    // get input
    Scanner input = new Scanner(inputFile);
    String line = input.nextLine();
    // calculate height of the array
    int height = line.length() / width;
    // Add one to height if there's a partial last row
    if (line.length() % width != 0)
        height += 1;

    loadUnloadGrid(line, width, height);
}
static void loadUnloadGrid(String line, int width, int height) {
    // make an empty array
    char grid[][] = new char[height][width];

    // fill the array row by row with character from line

    int charCount = 0;
    for (int r = 0; r < height - 1; r++) {
        for (int c = 0; c < width; c++) {
            // check to make sure accessing past end of the line
            if (charCount < line.length()) {
                grid[r][c] = line.charAt(charCount);
                charCount++;

            }
        }
    }

    // print to standard output the characters in array
    System.out.printf("Grid width %d: \"", width);
    for (int r = 0; r < width; r++) {
        for (int c = 0; c < height; c++) {
            System.out.print(grid[c][r]);
        }
    }
    // !!Special handling for last row!!
    int longColumn = line.length() % width;
    if (longColumn == 0)
        longColumn = width;

    for (int c = 0; c < longColumn; c++) {
        System.out.print(grid[height - 1][c]);
    }
    System.out.println();



}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
if(手动学习单元(参数))
System.out.println(“加密”);
//获取输入
扫描仪输入=新扫描仪(输入文件);
String line=input.nextLine();
//计算阵列的高度
int height=line.length()/width;
//如果有部分最后一行,则在高度中添加一个
if(line.length()%width!=0)
高度+=1;
加载网格(线、宽、高);
}
静态void loadUnloadGrid(字符串行、整数宽度、整数高度){
//空数组
字符网格[][]=新字符[高度][宽度];
//用第行中的字符逐行填充数组
int charCount=0;
对于(int r=0;r
在循环列之前,您需要循环行,也就是说,您通常不会这样做

for (int i = 0; i < array[0].length; i++) {
    for (int j = 0; j < array.length; j++) {
         // prints columns before rows
    }
}

编辑:我看到你的循环应该正常工作。很可能
height
只是
1
而不是
2
,因此,最后一行被切断。我的代码按原样用于输入,你的循环应该以同样的方式工作。试着打印
height
并检查它是否正确,或者调试你的程序,然后再试一次啊,一步一步。

您是否在IDE调试器中逐步完成了代码?这是开始的地方。
if (array[j].length > i)
    // System.out.println(...);
else
    break;