简单加密的 NumberFormatException

NumberFormatException with Simple Encryption

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 传递给 class,然后允许它加密和解密传递给它的任何字符串。例如:

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
    }

}