无法获得完整的输出。当我按原样运行我的程序时,它会执行所需的字符移位,但只对前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
答案 0 :(得分:0)
有几个问题,
你没有处理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]);
}
}
}