1

I simply have two editText views. One is in the main activity and one is in an alert dialog. The one in the main activity contains a string of numbers (i.e. 0.00) and my goal is to add whatever the user inputs from the second editText in the alert dialog to the number in the main activity.

I am trying to use BigDecimals to do the addition and here is my code in a onClickListnener

   private BigDecimal user_input;
   private BigDecimal original;
   private BigDecimal sum;
   protected void onCreate(Bundle savedInstanceState){
   .
   .
   .
   .
                 //here is the problem
                 user_input=new BigDecimal( editText_in_alert.getText().toString());
                 original  =new BigDecimal( editText_in_main_activity.getText().toString());
                 sum=user_input.add(original); 

Is that how we are supposed to convert a string into a BigDecimal and add them up together?

My app crashes when I click on a button but it works fine when I remove the BigDecimal operations...

here is the log :

      java.lang.NumberFormatException: Invalid long: " 000 "
        at java.lang.Long.invalidLong(Long.java:124)
        at java.lang.Long.parse(Long.java:363)
        at java.lang.Long.parseLong(Long.java:353)
        at java.lang.Long.parseLong(Long.java:321)
        at java.math.BigDecimal.<init>(BigDecimal.java:344)
        at java.math.BigDecimal.<init>(BigDecimal.java:425)
        at com.example.galaxy.expense.MainActivity$1.onClick(MainActivity.java:48)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
GalaxyVintage
  • 656
  • 1
  • 11
  • 20
  • If you are dealing with money or need correct precision then use BigDecimal otherwise you can use double also. – Pankaj May 23 '15 at 04:49
  • Yea I'm dealing money and people suggest using bigdecimal – GalaxyVintage May 23 '15 at 04:50
  • 1
    can you check your log and paste here where app getting crashed. – Pankaj May 23 '15 at 04:51
  • You mean the ADB logs? – GalaxyVintage May 23 '15 at 04:52
  • 1
    yeah can you show the logs and have you initialized your editText before getting values from them. – Pankaj May 23 '15 at 04:57
  • Have you tried [this answer](http://stackoverflow.com/a/18231943/817950) yet? Also, what are the values of `editText_in_alert` and `editText_in_main_activity` when it crashes? – redbmk May 23 '15 at 05:03
  • @redbmk is there a way to check? I am still new to this – GalaxyVintage May 23 '15 at 05:03
  • @Clairvoyant I added the log that is in red – GalaxyVintage May 23 '15 at 05:09
  • You could try running in Debug mode if you're using [Android Studio](https://developer.android.com/tools/debugging/debugging-studio.html) or [Eclipse](http://developer.android.com/tools/debugging/debugging-projects.html). Based on your error though, it looks like it's having issues parsing the string " 000 ". You could enforce proper formatting in the [text input](http://developer.android.com/guide/topics/ui/controls/text.html) (would using `android:inputType="number"` be good enough?), or catch `NumberFormatException`s and tell the user to modify their input. – redbmk May 23 '15 at 05:23
  • 1
    @redbmk thanks! I went back to check my xml file and I put the underline tags around the string that I want to parse and it is causing the problem. – GalaxyVintage May 23 '15 at 05:30
  • @Lzy ... just about your other(deleted) question: here is what you can do ... http://ideone.com/Y8iO3m (MyActivity1, MyActivity2, MyActivity3) – Selvin Jun 08 '15 at 08:07
  • @Selvin thanks for the code, I think I understand it better now – GalaxyVintage Jun 08 '15 at 08:15

1 Answers1

1

Apparently in my XML file I put the underline tags around 0.00 which gives the error when it is being converted to BigDecimal.. I removed the tags and everything works now

GalaxyVintage
  • 656
  • 1
  • 11
  • 20