首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当试图将char数组的元素转换为int时出错

当试图将char数组的元素转换为int时出错
EN

Stack Overflow用户
提问于 2018-05-28 12:57:18
回答 3查看 65关注 0票数 0

下面是我到目前为止使用的代码:

代码语言:javascript
复制
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;
}

当我试图运行它时,它正在运行,但是当我使用encrypt方法时,我得到了以下错误:

代码语言:javascript
复制
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)

我试过改变一些东西,比如改变它的字符转换成int的方式,但是似乎没有什么效果.我做错了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-28 13:00:51

如果您想通过对每个字符进行一定量的偏移来“加密”字符串,那么只需直接这样做:

代码语言:javascript
复制
String encrypted = null;
char passChars[] = password.toCharArray();
for (int i=0; i < passChars.length; i++) {
    passChars[i] += 17;
}
encrypted = new String(passChars);

Demo

请注意,在某些ASCII字符中添加17个字符可能会使您遇到一些不可打印的东西。这里还不清楚你想要实现什么;如果你想系统地把密码加到其他字符中,那么我们可能得用模数来包装。

票数 1
EN

Stack Overflow用户

发布于 2018-05-28 13:03:32

这句话是错的:

代码语言:javascript
复制
ascii[i] = Integer.parseInt(String.valueOf(passChars[i]));

你想把密码的字母转换成整数。不能保证密码只由数字组成。

相反,您可以这样做:

代码语言:javascript
复制
ascii[i] = passChars[i] + 17;

直接了当。

票数 0
EN

Stack Overflow用户

发布于 2018-05-28 13:09:39

用有效的大小初始化ascii[],否则,您可能会将索引排除在绑定异常之外,此程序将仅作为数字字符串用于密码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50567054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档