0

这是我的代码:

  TextView headerVal = (TextView) nextChild.findViewById(R.id.textView3); 
  String tt = ((String)headerVal.getText()).replaceAll(",",""); //it was 1,000 and it became 1000           
    int longueur = tt.length();
    String aux2 = (String) tt.substring(0,longueur - 2);
    int contenu = (int) Integer.parseInt(aux2);// here is the error "unable to  parse 1000 as an integer".

PS:它可以在模拟器上运行,但我在手机上遇到了这个错误

日志猫

04-04 13:04:15.210: E/AndroidRuntime(31718): FATAL EXCEPTION: main

04-04 13:04:15.210: E/AndroidRuntime(31718): java.lang.NumberFormatException: unable to parse '1 000' as integer

04-04 13:04:15.210: E/AndroidRuntime(31718):at java.lang.Integer.parse(Integer.java:433)

04-04 13:04:15.210: E/AndroidRuntime(31718):at java.lang.Integer.parseInt(Integer.java:422)

04-04 13:04:15.210: E/AndroidRuntime(31718):at java.lang.Integer.parseInt(Integer.java:382)
04-04 13:04:15.210:E/AndroidRuntime(31718):at com.example.appui.BarGraphActivity.setColumnParts(BarGraphActivity.java:408)
04-04 13:04:15.210: E/AndroidRuntime(31718):at com.example.appui.BarGraphActivity.access$2(BarGraphActivity.java:282)
04-04 13:04:15.210: E/AndroidRuntime(31718):at com.example.appui.BarGraphActivity$Desired_pension.onStopTrackingTouch(BarGraphActivity.java:162)
4

3 回答 3

0
Try this

public static void main(String[] args) {

        String test = ",100,0,0,";
        int newTest;
        test = test.replaceAll(",","");

        int longueur = test.length();
              // If the longueur length is greater than or equal to test.length() then it will return error, so put a check to see the longueur is not exceeding the test length.
        String aux2 = (String) test.substring(0,longueur - 2);

        newTest = Integer.parseInt(aux2);

        System.out.println(newTest);

    }

结果 100,如果 longueur - 1 返回 1000

于 2013-04-04T13:15:12.030 回答
0

您只需在替换后使用修剪功能:-

String str2 = str.trim();

上面的代码删除字符串中的所有空格,然后将字符串转换为整数:-

TextView headerVal = (TextView) nextChild.findViewById(R.id.textView3); 
String tt = ((String)headerVal.getText()).replaceAll(",",""); //it was 1,000 and it became 1000           

 String str2 = tt .trim();
int longueur = str2 .length();

然后做你想做的事情......

于 2013-04-04T13:18:13.553 回答
0

使用DecimalFormat如下:

 DecimalFormat format =(DecimalFormat) DecimalFormat.getInstance();
 format.applyPattern("#,###");
 try {
     integer = (Integer)format.parse(tt);
 } catch (ParseException e) {
     System.err.println(new Date()+": "+e.getMessage());
 }
于 2013-04-04T13:10:03.603 回答