我正在尝试将填充了16位数字的字符串转换为一个整数数组,其中每个索引都保存字符串中各自索引的数字.我正在编写一个程序,我需要对字符串中的单个int进行数学运算,但我尝试过的所有方法似乎都不起作用.我也不能用字符分割,因为用户正在输入数字.
这是我尝试过的.
//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);
}
Run Code Online (Sandbox Code Playgroud)
知道我应该怎么做吗?
这个怎么样:
for (int count = 0; count < userInput.length; ++count)
intArray[count] = userInput.charAt(count)-'0';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5630 次 |
| 最近记录: |