将String转换为int以获取唯一编号

时间:2013-06-21 16:24:39

标签: java android string int

嗨我正在尝试将诸如“abc-d1_23QWEwer”之类的刺痛转换为int数

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

6 个答案:

答案 0 :(得分:5)

观察:

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

我认为它会溢出,你可能会使用很长的。

答案 1 :(得分:2)

数字2,914,282,912,918,292,114太大而不适合int。 只是适合长期(签名长期的最大值是9,223,372,036,854,775,807),所以你可以做到

long l = Long.parseLong(someString);

虽然更好的解决方案是问问自己为什么需要如此庞大的数字。重新设计算法以避免出现这么大的数字会让事情变得更容易。

答案 2 :(得分:1)

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

numServiceName = Long.parseLong(stringServiceValue);

答案 3 :(得分:1)

您的号码2914282912918292114太大,无法成为int。可以容纳的最大值int2,147,483,647。您需要使用long 9,223,372,036,854,775,807,其最大值可以为 Long.parseLong(yourString);

{{1}}

如果您处理大量数字,可以使用BigInteger类作为整数,使用BigDecimal作为带小数位的数字。

答案 4 :(得分:1)

使用long而不是整数。你的号码太大了 如果你需要一个不长的int

,你可以做这样的事情
sb.append(String.valueOf(Character.getNumericValue(serviceName.charAt(i))).substring(0, 1));

答案 5 :(得分: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]);