【问题标题】:Shifting characters in a string, not getting complete output移动字符串中的字符,没有得到完整的输出
【发布时间】:2015-12-21 11:15:48
【问题描述】:

无法获得完整的输出。当我按原样运行我的程序时,它会执行所需的字符转换,但仅针对前 16 个字符,之后它只会输出 null 或者我得到一个超出范围的索引。

我已经尝试改变读取字符串的长度,但我被卡住了,不知道从哪里开始,有什么建议吗?你将如何展示如何打印剩余的字符串?

package com.sanfoundry.setandstring;

import java.io.IOException;

public class shiftRows {
    /*
     *  The first row remains the same. Shift the second row one position to the left. 
     *  Shifts the third row two positions to the left. Finally, shift the fourth row
     *  three positions to the left.
     */


    public static String shiftRows(String str, int shiftNum)
    {
        char[] out = new char[4];
        if(shiftNum==1)
        {
            out[0]=str.charAt(1); //shifts char at position 0 
            out[1]=str.charAt(2); //shifts char at position 1 to pos 2
            out[2]=str.charAt(3); //shifts char at position 2 to pos 3
            out[3]=str.charAt(0); //shifts char at position 3 to pos 1
        }
        if(shiftNum==2)
        {
            out[0]=str.charAt(2);
            out[1]=str.charAt(3);
            out[2]=str.charAt(0);
            out[3]=str.charAt(1);
        }
        if(shiftNum==3)
        {
            out[0]=str.charAt(3);
            out[1]=str.charAt(0);
            out[2]=str.charAt(1);
            out[3]=str.charAt(2);
        }
        return new String(out);
    }

    public static void main(String[] args) throws IOException
    {
        System.out.println("SHIFT ROWS: " );

        String[] input= new String[16]; //string prints 4x4 block of 16char

        String[] output= new String[16]; //outputs in 4x4 block

        String random = "AJUYJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFASDF"; //random string

        for (int i = 0, n = 0; i < 4; i++, n+=4) { //i<4
            input[i] = random.substring(0+n, 4+n); //reads length of random string and performs shift
        }

        output[0] = input[0];
        for(int i=1; i<4; i++)//i<4
        {
            output[i] = shiftRows(input[i],i);
        }

        for(int i=0; i<16; i++) //loops to print in 4x4 block
        {
            System.out.println(output[i]); //prints the output
        }

    }
}

我的输出:

AJUY
TXQJ
HXHU
YICW
null
null
null
null
null
null
null
null
null
null
null
null

desired output:

AJUY
TXQJ
HXHU
YICW
YMGH
RKQT
PWHQ
GKYV
LPYS
WGOP
TOIN
MFOP
OAAA
AAAA
AAAA
AAAA

【问题讨论】:

  • 你能给出你传递给方法的示例字符串吗?
  • 随机字符串 = "AJUYJTXQHUHCICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFASDF";
  • for(int i=1; i&lt;4; i++) - 你想移动 16 个字符串,为什么只循环其中的 3 个?

标签: java arrays char


【解决方案1】:

有几个问题,

你没有处理 shiftNum 0 分割字符串的for循环不对

请参阅下面提供所需结果的代码

package com.sanfoundry.setandstring;

import java.io.IOException;

public class shiftRows {
    public static String shiftRows(String str, int shiftNum) {
        char[] out = new char[4];
        if (shiftNum == 0) {
            out = str.toCharArray();
        }
        if (shiftNum == 1) {
            out[0] = str.charAt(1); //shifts char at position 0
            out[1] = str.charAt(2); //shifts char at position 1 to pos 2
            out[2] = str.charAt(3); //shifts char at position 2 to pos 3
            out[3] = str.charAt(0); //shifts char at position 3 to pos 1
        }
        if (shiftNum == 2) {
            out[0] = str.charAt(2);
            out[1] = str.charAt(3);
            out[2] = str.charAt(0);
            out[3] = str.charAt(1);
        }
        if (shiftNum == 3) {
            out[0] = str.charAt(3);
            out[1] = str.charAt(0);
            out[2] = str.charAt(1);
            out[3] = str.charAt(2);
        }
        return new String(out);
    }

    public static void main(String[] args) throws IOException {
        System.out.println("SHIFT ROWS: ");
        String[] output = new String[16];
        String random = "AJUYJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFASDF";

        for (int i = 0, n = 0; n < random.length() - 1; i++, n += 4) {
            String substring = random.substring(n, 4 + n);
            if (i == 0) {
                output[0] = substring;
            } else {
                output[i] = shiftRows(substring, i % 4);
            }
        }

        for (int i = 0; i < 16; i++) {
            System.out.println(output[i]);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2015-06-20
    • 2017-01-07
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    相关资源
    最近更新 更多