整数解析器字符串

时间:2012-11-24 05:27:00

标签: java android parsing integer

  

这是我转换为整数

的代码
 String pricevalue=i.getStringExtra("info");
        int start = pricevalue.indexOf("price:");
       int index=pricevalue.indexOf("$");

       price=pricevalue.subSequence(index,pricevalue.length()-1).toString();



 int p1=Integer.parseInt(price.toString());//error is here
  

我的日志cat错误

11-24 10:51:32.002: E/AndroidRuntime(549): FATAL EXCEPTION: main
11-24 10:51:32.002: E/AndroidRuntime(549): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.orderplacemnet/com.example.orderplacemnet.YourTotalBill}: java.lang.NumberFormatException: unable to parse '$34.00' as integer
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.os.Looper.loop(Looper.java:123)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.reflect.Method.invokeNative(Native Method)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.reflect.Method.invoke(Method.java:521)
11-24 10:51:32.002: E/AndroidRuntime(549):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-24 10:51:32.002: E/AndroidRuntime(549):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-24 10:51:32.002: E/AndroidRuntime(549):  at dalvik.system.NativeStart.main(Native Method)
11-24 10:51:32.002: E/AndroidRuntime(549): Caused by: java.lang.NumberFormatException: unable to parse '$34.00' as integer
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.Integer.parse(Integer.java:433)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.Integer.parseInt(Integer.java:422)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.Integer.parseInt(Integer.java:382)
11-24 10:51:32.002: E/AndroidRuntime(549):  at com.example.orderplacemnet.YourTotalBill.onCreate(YourTotalBill.java:51)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-24 10:51:32.002: E/AndroidRuntime(549):  ... 11 more

2 个答案:

答案 0 :(得分:1)

price=pricevalue.subSequence(index+1,pricevalue.length()-1);
Double p1=Double.parseDouble(price);

而不是

price=pricevalue.subSequence(index,pricevalue.length()-1).toString();
int p1=Integer.parseInt(price.toString());

可能会解决您的问题。

答案 1 :(得分:0)

我改变代码现在试试

String pricevalue=i.getStringExtra("info");
int start = pricevalue.indexOf("price:");
int index=pricevalue.indexOf("$");
price=pricevalue.subSequence(index,pricevalue.length()-1).toString();
int p1=Integer.parseInt(price.replaceAll("[\\D]", ""))