0

This is my code :

  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: it works on the emulator but i got this error on my phone

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)
user2137817
  • 1,825
  • 5
  • 28
  • 45

3 Answers3

0

Use DecimalFormat as below:

 DecimalFormat format =(DecimalFormat) DecimalFormat.getInstance();
 format.applyPattern("#,###");
 try {
     integer = (Integer)format.parse(tt);
 } catch (ParseException e) {
     System.err.println(new Date()+": "+e.getMessage());
 }
hd1
  • 33,938
  • 5
  • 80
  • 91
  • Post your entire code, it works for me, must be something silly – hd1 Apr 04 '13 at 13:23
  • can't post the whole code because it's too long but regarding the parse,it's the full code after that i use value i parsed for some operations – user2137817 Apr 04 '13 at 13:34
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);

    }

Result 100, if longueur - 1 Returns 1000

DAS
  • 686
  • 8
  • 13
0

You just use trim function after replace :-

String str2 = str.trim();

Above code remove all space into your string then convert string into integer:-

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

Then do ur task what you want....

duggu
  • 37,851
  • 12
  • 116
  • 113