【问题标题】:error while parsing an integer解析整数时出错
【发布时间】:2013-03-26 13:40:30
【问题描述】:

这是我的代码:

  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:它可以在模拟器上运行,但我在手机上遇到了这个错误

Logcat

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)

【问题讨论】:

  • 它显示了什么??任何错误??
  • 无法将 1000 解析为整数
  • @Sebastian 不明白你的想法,请详细说明
  • 可能是trimming的问题
  • 为什么在 Integer 解析值后在最后一条语句中类型转换 'int' ?

标签: android parsing


【解决方案1】:
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

【讨论】:

    【解决方案2】:

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

    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();
    

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

    【讨论】:

    【解决方案3】:

    如下使用DecimalFormat

     DecimalFormat format =(DecimalFormat) DecimalFormat.getInstance();
     format.applyPattern("#,###");
     try {
         integer = (Integer)format.parse(tt);
     } catch (ParseException e) {
         System.err.println(new Date()+": "+e.getMessage());
     }
    

    【讨论】:

    • 发布你的整个代码,它对我有用,一定很傻
    • 无法发布整个代码,因为它太长了,但是关于解析,它是完整代码之后我使用我为某些操作解析的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多