0

嗨,我正在尝试将诸如“abc-d1_23QWEwer”之类的字符串转换为整数

StringBuilder sb = new StringBuilder();
int intServiceName = 0;
String stringServiceValue;
for (int i = 0; i < serviceName.length(); i++){
    if (DEBUG) Log.i(TAG, "serviceName.length() loop i: " + i);
    sb.append(String.valueOf(Character.getNumericValue(serviceName.charAt(i))));
    if (DEBUG) Log.i(TAG, "serviceName.length() loop i after: " + i);
}
if (DEBUG) Log.i(TAG, "serviceName.length() loop end2");
stringServiceValue = sb.toString();
if (DEBUG) Log.i(TAG, "serviceName.length() loop end: "+stringServiceValue.replaceAll("\\D+",""));
stringServiceValue = stringServiceValue.replaceAll("\\D+","");
if (DEBUG) Log.i(TAG, "serviceName.length() loop endstringServiceValue: "+ stringServiceValue);
intServiceName = Integer.parseInt(stringServiceValue);
if (DEBUG) Log.i(TAG, "serviceName.length() loop end123123: "+ String.valueOf(intServiceName));

当代码到达这一行时,我收到一个错误:

intServiceName = Integer.parseInt(stringServiceValue);

我究竟做错了什么?这是错误日志

06-21 19:12:54.760: E/AndroidRuntime(11139): FATAL EXCEPTION: AsyncTask #4
06-21 19:12:54.760: E/AndroidRuntime(11139): java.lang.RuntimeException: An error occured while executing doInBackground()
06-21 19:12:54.760: E/AndroidRuntime(11139):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Thread.run(Thread.java:856)
06-21 19:12:54.760: E/AndroidRuntime(11139): Caused by: java.lang.NumberFormatException: Invalid int: "2914282912918292114"
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.invalidInt(Integer.java:138)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.parse(Integer.java:378)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.parseInt(Integer.java:366)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.parseInt(Integer.java:332)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at com.wr.noc.AsyncTasks.doInBackground(AsyncTasks.java:113)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at com.wr.noc.AsyncTasks.doInBackground(AsyncTasks.java:1)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-21 19:12:54.760: E/AndroidRuntime(11139):    ... 5 more
4

6 回答 6

5

观察 :

06-21 19:12:54.760: E/AndroidRuntime(11139): Caused by: java.lang.NumberFormatException: Invalid int: "2914282912918292114"

我认为它会溢出,您可以使用 long 代替。

于 2013-06-21T16:27:58.207 回答
2

数字 2,914,282,912,918,292,114 太大而无法放入 int 中。它只适合一个长整数(有符号长整数的最大值是 9,223,372,036,854,775,807),所以你可以这样

long l = Long.parseLong(someString);

尽管更好的解决方案是问问自己为什么需要如此庞大的数字。重新设计您的算法以避免拥有如此大的数字将使您继续进行时更容易。

于 2013-06-21T16:28:47.883 回答
1

您的号码2914282912918292114太大而不能成为int. int能容纳的最大值是2,147,483,647。您需要使用long可以保持最大值的9,223,372,036,854,775,807.

 Long.parseLong(yourString);

如果您正在处理大量数字,则可以将BigInteger类用于整数和BigDecimal带有十进制数字的数字。

于 2013-06-21T16:30:51.920 回答
1

使用 long 而不是整数。你的数字太大了,如果你需要一个不长的 int,你可以做这样的事情

sb.append(String.valueOf(Character.getNumericValue(serviceName.charAt(i))).substring(0, 1));
于 2013-06-21T16:30:53.640 回答
1

您的数字对于 int 来说太大了。考虑使用 long :

numServiceName = Long.parseLong(stringServiceValue);
于 2013-06-21T16:28:15.550 回答
0

像这样遍历整个字符串:

// not sure if the syntax is correct

int myInt[] = new int[myString.length()];

for (int i = 0; i < myString.length(); i++)
    myInt.add(myString[i]);
于 2013-06-21T16:31:07.907 回答