MilanJ
MilanJ

Reputation: 5

Getting an error when trying to convert an element of a char array to int

Below is the code I have used so far:

public static String encrypt(String password) {
    String encrypted = null;
    char passChars[] = password.toCharArray();
    int ascii[] = null;
    for(int i=0;i<passChars.length;i++) {
        ascii[i] = Integer.parseInt(String.valueOf(passChars[i]));
        ascii[i] = ascii[i] + 17;
        passChars[i] = (char)ascii[i];
        encrypted = encrypted + String.valueOf(passChars[i]);
    }
    return encrypted;
}

When I try to run this, it is getting run but when I use the encrypt method, I am getting following error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "M"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.package.program.Encryptor.encrypt(Encrypter.java:46)
at com.package.program.Main$3.actionPerformed(Main.java:136)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

I've tried changing a few things, such as changing the way it char is being converted to int but nothing seemed to work... What am I doing wrong?

Upvotes: 0

Views: 71

Answers (3)

singh09iet
singh09iet

Reputation: 11

Initialize ascii[] with valid size other wise you may get index out of bound exception and this program will work only for password as a numeric string only.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521103

If you want to "encrypt" the string by just offsetting each character by some amount, then just do that directly:

String encrypted = null;
char passChars[] = password.toCharArray();
for (int i=0; i < passChars.length; i++) {
    passChars[i] += 17;
}
encrypted = new String(passChars);

enter image description here

Demo

Note that adding 17 to certain ASCII characters may land you on something which is not printable. It isn't clear what you're trying to achieve here; if you want to systematically scramble a password into other characters, then we would have to wrap around using the modulus perhaps.

Upvotes: 1

Ahmad Shahwan
Ahmad Shahwan

Reputation: 1957

This line is wrong:

ascii[i] = Integer.parseInt(String.valueOf(passChars[i]));

You're trying to convert the letters of your password, to integers. There is no guarantee that the password is composed of only numbers.

Instead, you can do:

ascii[i] = passChars[i] + 17;

directly.

Upvotes: 0

Related Questions