Gamer Guy
Gamer Guy

Reputation: 21

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);
    }

}

I have a question about encryption. This program is supposed to receive a string and a number and increase each char by this number. This is not working. It receives the number properly but does not add the chars correctly. I am also receiving the error of an exception in the thread "main": for input string: ""

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)

Upvotes: 1

Views: 479

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

This is not good:

    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));
    }

You're calling idolnum() multiple times within the for loop, including after you've extracted the number String from the original String, and so when you do this a second time, you get the NumberFormatException for trying to parse "". Instead of doing this, call diolnum() once and only once, and before the for loop. Then use the encry int whenever the encryption int is needed.

Note that if this were my project, I'd organize it differently. I'd only pass the encryption int to the class, and then allow it to encrypt and decrypt whatever Strings are passed into it. For example:

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
    }

}

Upvotes: 1

Related Questions