gpt4 book ai didi

java - 带有简单加密的 NumberFormatException

转载 作者:行者123 更新时间:2023-11-30 07:50:11 24 4
gpt4 key购买 nike

package codeCracker;

public class CodeCracker {
private String encrypt;
private int encry;

public CodeCracker(String encryptmelong) {
encrypt = encryptmelong;
}

public String idolnum() {
String in;
in = encrypt;
in = in.replaceAll("\\D+", "");
encry = Integer.valueOf(in);
encrypt = encrypt.replace(in, "");
// System.out.println(encry);
return in;
}

public String encrypt() {
// encrypt+=encry;

String encrypted = "";
String charen = "";
for (int i = 0; i < encrypt.length(); i++) {
charen += encrypt.charAt(i);
// System.out.println(charen.charAt(i));
}
for (int i = 0; i < charen.length(); i++) {
System.out.println(encry);
int temp = Integer.parseInt(idolnum());
System.out.println("" + temp + " " + (int) encrypt.charAt(i));
temp = (int) encrypt.charAt(i) + temp;
encrypted = encrypted + (char) temp;
// System.out.println(encrypted.charAt(i));
}
return encrypted;

}

public String toString() {
return encrypt();
}

public static void main(String[] args) {
CodeCracker code = new CodeCracker("5 encryptme");
System.out.println(code);
}

}

我有一个关于加密的问题。该程序应该接收一个字符串和一个数字,并将每个字符增加该数字。这是行不通的。它正确接收数字,但未正确添加字符。我还在线程“main”中收到异常错误:对于输入字符串:“”

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at
Java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at
java.lang.Integer.parseInt(Integer.java:504) at
java.lang.Integer.valueOf(Integer.java:582) at
codeCracker.CodeCracker.idolnum(CodeCracker.java:15) at
codeCracker.CodeCracker.encrypt(CodeCracker.java:34) at
codeCracker.CodeCracker.toString(CodeCracker.java:44) at
java.lang.String.valueOf(String.java:2854) at
java.io.PrintStream.println(PrintStream.java:821) at
codeCracker.CodeCracker.main(CodeCracker.java:50)

最佳答案

这不好:

    for (int i = 0; i < charen.length(); i++) {
System.out.println(encry);
int temp = Integer.parseInt(idolnum());
System.out.println("" + temp + " " + (int) encrypt.charAt(i));
temp = (int) encrypt.charAt(i) + temp;
encrypted = encrypted + (char) temp;
// System.out.println(encrypted.charAt(i));
}

您在 for 循环中多次调用 idolnum() ,包括在从原始字符串中提取数字字符串之后,因此当您第二次执行此操作时,您会得到用于尝试解析“”的 NumberFormatException。不要这样做,而是在 for 循环之前调用一次且仅一次 diolnum() 。然后在需要加密 int 时使用 encry int。

请注意,如果这是我的项目,我会以不同的方式组织它。我只会将加密 int 传递给该类,然后允许它加密和解密传递给它的任何字符串。例如:

public class MyCodeCracker {
private int encry;

public MyCodeCracker(int encry) {
this.encry = encry;
}

public String encrypt(String text) {
// use encry to do encrytion
return ""; // return encrypted text
}

public String decrypt(String encryptedText) {
// use encry to translate encryptedText to text
return ""; // return text
}

public int getEncry() {
return encry;
}

public static void main(String[] args) {
// here get user input
// extract out the encryption int
// create MyCodeCracker with the int
// and then encrypt and decrypt text as needed
}

}

关于java - 带有简单加密的 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33402968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com