java - 在Java中将数字存储到字符串数组的问题
问题描述
我正在尝试将异或运算的结果存储到字符串数组中。当我如图所示运行我的代码时,它能够打印出这些值。
int[] randnumbs = new int[0];
randnumbs = streamGen(plaintext.length());
String plaintextnumb;
String encryption;
int[] answer = new int[0];
int count = 0;
while(true) {
plaintextnumb = plaintext.substring(count, count + 2);
int numbfromfile = Integer.valueOf(plaintextnumb, 16);
int xored = numbfromfile ^ randnumbs[count];
encryption = Integer.toHexString(xored);
System.out.print(encryption + " ");
if(count == (plaintext.length() / 2) - 1) {
break;
}else {
count++;
}
}
结果:
af a0 52 b1 fb 0 a6 75
当我将变量“加密”更改为字符串数组时,我的代码能够运行,但是当它到达“加密 [count] = Integer.toHexString(xored);”时,接缝停止运行 我以前从未遇到过这个问题。当我运行我的程序时没有出现任何错误,它只是显示一个空的控制台。我还在这行代码之前和之后插入了打印输出语句,并且只能在代码行之前看到打印输出,而不是之后。任何解释为什么会发生这种情况将不胜感激!
int[] randnumbs = new int[0];
randnumbs = streamGen(plaintext.length());
String plaintextnumb;
String[] encryption = new String[0];
int[] answer = new int[0];
int count = 0;
while(true) {
plaintextnumb = plaintext.substring(count, count + 2);
int numbfromfile = Integer.valueOf(plaintextnumb, 16);
int xored = numbfromfile ^ randnumbs[count];
encryption[count] = Integer.toHexString(xored);
System.out.print(encryption[count] + " ");
if(count == (plaintext.length() / 2) - 1) {
break;
}else {
count++;
}
}
解决方案
问题是数组的大小encryption
是0
因为String[] encryption = new String[0];
声明。因此,使用语句赋值,encryption[count] = Integer.toHexString(xored);
可能会导致ArrayIndexOutOfBoundsException
因为 的值count
可能已经增加(可能已经超过0
)。
一个解决方案是用所需的大小声明你的数组,例如String[] encryption = new String[10];
另外,请检查您是否在该行放置了一个调试点,encryption[count] = Integer.toHexString(xored);
以及您是否在调试模式下运行程序。如果是,这可能是您的程序没有超出这条线的原因。
推荐阅读
- html - 如何在 python selenium webdriver 中选择树节点跨度标记值?
- python - Pandas - 用增加的后缀标记列表中的重复元素
- android - 安卓进程
- python - 在 Python 3.8 上安装 XGBoost 的问题
- java - 拒绝值 [org.springframework.web.multipart.support
- c# - Log4Net c# AdoNetAdapter 附加字段
- persistence - 当订阅者应用程序关闭时,emqttd 未获取同一主题中的所有消息
- javascript - 优化具有多个子节点的元素上的平移动画
- html - 从内容页面在母版页中添加元素
- java - JPA 选择关联并使用 NamedEntityGraph