我正在尝试将一个填充有 16 位数字的字符串转换为一个整数数组,其中每个索引都保存其在字符串中相应索引的数字。我正在编写一个程序,我需要对字符串中的单个整数进行数学运算,但我尝试过的所有方法似乎都不起作用。我也不能按字符分割,因为用户正在输入数字。
这是我尝试过的。
//Directly converting from char to int
//(returns different values like 49 instead of 1?)
//I also tried converting to an array of char, which worked,
//but then when I converted
//the array of char to an array of ints, it still gave me weird numbers.
for (int count = 0; count <=15; count++)
{
intArray[count] = UserInput.charAt(count);
}
//Converting the string to an int and then using division to grab each digit,
//but it throws the following error (perhaps it's too long?):
// "java.lang.NumberFormatException: For input string: "1234567890123456""
int varX = Integer.parseInt(UserInput);
int varY = 1;
for (count=0; count<=15; count++)
{
intArray[count]= (varX / varY * 10);
}
知道我应该怎么做吗?