0

Is it possible to convert strings like this +37068941556 to integers or longs or whatever numeric formats (in this case I would like to convert it to number 37068941556. Thanks in advance!

Stack trace of the error I get when trying with parseLong():

04-24 20:18:46.253: E/AndroidRuntime(31258): FATAL EXCEPTION: main
04-24 20:18:46.253: E/AndroidRuntime(31258): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nrsearch/com.example.nrsearch.MainActivity}: java.lang.NumberFormatException: Invalid long: "+37068941556"
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.os.Looper.loop(Looper.java:176)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.reflect.Method.invoke(Method.java:525)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at dalvik.system.NativeStart.main(Native Method)
04-24 20:18:46.253: E/AndroidRuntime(31258): Caused by: java.lang.NumberFormatException: Invalid long: "+37068941556"
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.invalidLong(Long.java:125)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.parse(Long.java:362)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.parseLong(Long.java:353)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.parseLong(Long.java:319)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at com.example.nrsearch.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:94)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.Activity.performStart(Activity.java:5382)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
04-24 20:18:46.253: E/AndroidRuntime(31258):    ... 11 more
Salivan
  • 1,876
  • 7
  • 26
  • 45
  • Yes, it is possible. How about reading the documentation of String, Integer and Long? – JB Nizet Apr 24 '14 at 17:15
  • @SotiriosDelimanolis No, not a duplicate at all, because my string has another characters, not only numbers, which makes it more complex. – Salivan Apr 24 '14 at 17:16
  • Other than the `+`, what other characters does it contain? What rules do you have for converting it? – Sotirios Delimanolis Apr 24 '14 at 17:17
  • It doesn't have any other characters but it has `+` and `parseLong()` gives me an error: `Invalid long`, so, as you can see, my question is not a duplicate and really needs a proper answer. – Salivan Apr 24 '14 at 17:21
  • What `String` value are you using? The `+` should not be an issue. Also, read the javadoc. – Sotirios Delimanolis Apr 24 '14 at 17:22
  • The `String` value I'm using is `+37068941556`. I do like this: `String nr = "+37068941556"; long number = Long.parseLong(nr);` and it gives me the error. – Salivan Apr 24 '14 at 17:24
  • Post the full stack trace. It works fine [here](http://ideone.com/Gm4bCU). – Sotirios Delimanolis Apr 24 '14 at 17:26
  • Have you considered reading the error message, or at least posting it if you can't understand it? Don't you think it might help diagnosing what the problem is? – JB Nizet Apr 24 '14 at 17:28
  • I've just edited my post. – Salivan Apr 24 '14 at 17:29
  • Maybe you `String` contains some invisible characters that are messing with everything. – Sotirios Delimanolis Apr 24 '14 at 17:33
  • 1
    Does your string end with some special character? If I am not wrong, Android uses Java6 and at its Lonng class reference (http://docs.oracle.com/javase/6/docs/api/java/lang/Long.html#parseLong(java.lang.String)) you can find aren't supported L ('\u004C')('\u006C') – Pablo Francisco Pérez Hidalgo Apr 24 '14 at 17:34
  • 1
    It appears that the Android version of `parseLong` may not recognize `+`. See this [doc](http://developer.android.com/reference/java/lang/Long.html#parseLong(java.lang.String)), which is different from Oracle's SE7 documentation. – ajb Apr 24 '14 at 17:34
  • Invisible characters? For example? – Salivan Apr 24 '14 at 17:35
  • 1
    It looks like `parseLong` in SE6 doesn't recognize `+` either, at least according to the javadoc. I haven't looked at the sources for either SE6 or Android, though. – ajb Apr 24 '14 at 17:37
  • Thanks, I already found my answer. I used `replaceAll("\\+", "")` as @Petro advised. – Salivan Apr 24 '14 at 17:38

1 Answers1

1

Try this:

String text = "+37068941556";
System.out.println(text.replaceAll("\\+", ""));
Petro
  • 3,484
  • 3
  • 32
  • 59