Java 如何使用2d数组加密和解密字符串?
实际上,我使用2d数组对普通字符串进行了加密和解密 下面是我的加密代码:Java 如何使用2d数组加密和解密字符串?,java,arrays,encryption,Java,Arrays,Encryption,实际上,我使用2d数组对普通字符串进行了加密和解密 下面是我的加密代码: import java.util.Scanner; public class Secret { public static void main(String[] args) { Scanner x = new Scanner(System.in); System.out.println("Enter a sentence that need to b
import java.util.Scanner;
public class Secret {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println("Enter a sentence that need to be encrypt : ");
String y = x.nextLine();
int len = y.length();
System.out.println("The length of the sentences is : " + len);
System.out.print("Enter number of column : " );
int arr = x.nextInt();
char [] [] z = new char [6] [arr];
// fill the character into array //
int pos = 0;
for (int i = 0; i<z.length; i++) {
for (int j = 0; j<z[i].length; j++) {
z[i][j] = y.charAt(pos);
pos++;
}
}
// output the encrypted text by reading the array downward, column by
column.//
for (int j = 0; j<z[arr].length; j++) {
System.out.print(z[0][j] + "" + z[1][j] + "" + z[2][j] + "" + z[3][j] + "" + z[4][j]
+ "" + z[5][j]);}
}
}
输出:
HloWrdel ol.
Hello World.
但是我想要的输出是这样的HloWrdel*ol…
,这意味着空间被“*”
替换,如果有额外的空数组,则打印“
”
现在,我的数组大小取决于字符串长度,所以当我输入arr=3(6x3=18)时,它会显示如下错误
Enter a sentence that need to be encrypt :
Hello World.
The length of the sentences is : 12
Enter number of column : 3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Secret.main(Secret.java:33)
因此,我的问题是如何得到这样的理想输出HloWrdel*ol…
?空间变为“*”
,输入数组大小没有限制。当我输入大于字符串长度的数组大小(列)时,字符串的字符将自动填充数组,多余的空数组将打印“
”
对于解密代码:
import java.util.Scanner;
public class Secret1 {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println("Enter the sentences that need to decrypt : ");
String str = x.nextLine();
int len = str.length();
System.out.println("The number of length is " + len);
System.out.println("Enter the number of rows : " );
int arr = x.nextInt();
char [][] z = new char [arr][6];
// fill character into array //
int pos = 0;
for(int i = 0; i<z.length; i++) {
for(int j = 0; j<z[i].length; j++) {
z[i][j] = str.charAt(pos);
pos++ ;
}
}
//output the decrypted text by reading the array from left to right, row by
row.//
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][0]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][1]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][2]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][3]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][4]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][5]);
}
}
}
输出:
HloWrdel ol.
Hello World.
但是我想要的输出是从加密文本HloWrdel*ol…
到纯文本Hello World.
。我如何获得这样的输出
此外,解密代码还取决于字符串长度,因此当我输入大于字符串长度的数组大小时,它显示如下:
Enter the sentences that need to decrypt :
HloWrdel ol.
The number of length is 12
Enter the number of rows :
3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Secret1.main(Secret1.java:147)
我输入的数组大小是否可能不限于字符串长度,而是在填充到数组中后不为额外的空数组打印任何内容?在最里面的循环中(对于(int j=0;j=len)的
,如果是,请跳过尝试读取2D元素并用“.”填充
if (pos>=len)
{
z[i][j]='.';
continue;
}
//your normal code
然后在填充元素后,检查是否刚刚填充了空间
if (z[i][j]==' ')
z[i][j]='*';