0

I've built a program that take some number and prints it with %. But if I input more than 4 digits the program crashes.

public void onTextChanged(CharSequence s, int start, int before, int count) {
  double currentBillTotal=0.0;
  double temp=0.0;
  if(s.length()>0){
    NumberFormat nf = NumberFormat.getInstance(); // get instance
    nf.setMaximumFractionDigits(2); // set decimal places

    currentBillTotal = Double.parseDouble(s.toString());                    
    String st=nf.format(currentBillTotal);
    currentBillTotal=Double.parseDouble(st);

    temp=currentBillTotal*0.1;
    st=nf.format(temp);
    temp=Double.parseDouble(st);
    tip10EditText.setText(st);
    total10EditText.setText(currentBillTotal+temp+"");

    temp=currentBillTotal*0.15;
    st=nf.format(temp);
    temp=Double.parseDouble(st);                    
    tip15EditText.setText(st);
    total15EditText.setText(currentBillTotal+temp+"");

    temp=currentBillTotal*0.2;
    st=nf.format(temp);
    temp=Double.parseDouble(st);
    tip20EditText.setText(st);  
    total20EditText.setText(currentBillTotal+temp+"");

  }else{
    currentBillTotal = 0.0;
    tip10EditText.setText((currentBillTotal*0.1)+"");
    total10EditText.setText(currentBillTotal+(currentBillTotal*0.1)+"");
    tip15EditText.setText((currentBillTotal*1.5)+"");
    total15EditText.setText(currentBillTotal+(currentBillTotal*0.15)+"");
    tip20EditText.setText(currentBillTotal*0.2+""); 
    total20EditText.setText(currentBillTotal+(currentBillTotal*0.2)+"");
  }

the logcat-

 FATAL EXCEPTION: main
 java.lang.NumberFormatException: Invalid double: "3,332"
   at java.lang.StringToReal.invalidReal(StringToReal.java:63)
   at java.lang.StringToReal.parseDouble(StringToReal.java:269)
   at java.lang.Double.parseDouble(Double.java:295)
   at com.example.homework24_11.MainActivity$1.onTextChanged(MainActivity.java:66)
   at android.widget.TextView.sendOnTextChanged(TextView.java:7092)
   at android.widget.TextView.handleTextChanged(TextView.java:7151)
   at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8794)
   at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
   at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
   at android.widget.TextView.doKeyDown(TextView.java:5246)
   at android.widget.TextView.onKeyDown(TextView.java:5065)
   at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
   at android.view.View.dispatchKeyEvent(View.java:7198)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
   at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
   at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
   at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
   at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5039)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)
 FATAL EXCEPTION: main
 java.lang.NumberFormatException: Invalid double: "1,111"
   at java.lang.StringToReal.invalidReal(StringToReal.java:63)
   at java.lang.StringToReal.parseDouble(StringToReal.java:269)
   at java.lang.Double.parseDouble(Double.java:295)
   at com.example.homework24_11.MainActivity$1.onTextChanged(MainActivity.java:66)
   at android.widget.TextView.sendOnTextChanged(TextView.java:7092)
   at android.widget.TextView.handleTextChanged(TextView.java:7151)
   at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8794)
   at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
   at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
   at android.widget.TextView.doKeyDown(TextView.java:5246)
   at android.widget.TextView.onKeyDown(TextView.java:5065)
   at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
   at android.view.View.dispatchKeyEvent(View.java:7198)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
   at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
   at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
   at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
   at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5039)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)
 FATAL EXCEPTION: main
 java.lang.NumberFormatException: Invalid double: "3,333"
   at java.lang.StringToReal.invalidReal(StringToReal.java:63)
   at java.lang.StringToReal.parseDouble(StringToReal.java:269)
   at java.lang.Double.parseDouble(Double.java:295)
   at com.example.homework24_11.MainActivity$1.onTextChanged(MainActivity.java:66)
   at android.widget.TextView.sendOnTextChanged(TextView.java:7092)
   at android.widget.TextView.handleTextChanged(TextView.java:7151)
   at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8794)
   at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
   at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
   at android.widget.TextView.doKeyDown(TextView.java:5246)
   at android.widget.TextView.onKeyDown(TextView.java:5065)
   at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
   at android.view.View.dispatchKeyEvent(View.java:7198)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
   at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
   at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
   at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
   at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5039)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)

How can I fix it? Why does the program crash after 4 digits? With less it works just fine.

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
Gili Yaniv
  • 3,073
  • 2
  • 19
  • 34
  • 6
    Post the logcat of the crash. – codeMagic Nov 26 '13 at 16:09
  • i add it... i dont know how to make it readable...lol – Gili Yaniv Nov 26 '13 at 16:22
  • 1
    `java.lang.NumberFormatException: Invalid double: "1,111"` It may be because you used a comma. The parser may not be liking that. – Jaiesh_bhai Nov 26 '13 at 16:25
  • but id didnt add the comma... i just wrote at the edit text the number 1111. it could be that from some how the program add it automatcly after 4 digits? or maybe its just like that at the console. – Gili Yaniv Nov 26 '13 at 16:28
  • 1
    [See the answer here](http://stackoverflow.com/questions/4323599/best-way-to-parsedouble-with-comma-as-decimal-separator) – codeMagic Nov 26 '13 at 16:30

2 Answers2

1

NumberFormat is a localized class while Double.parseDouble is not. So in your case because the locale on your system is not english, there's another formatting used, but Double.parseDouble expects english. 2 ways to solve it:

  1. Use NumberFormat nf = NumberFormat.getInstance(Locale.EN_US) to create your number format - Obviously not what you want.
  2. Use nf.parse(st) to parse. This parses the localizied string and should be more useful.
Bernd Ebertz
  • 1,317
  • 8
  • 10
0

Ive found a batter way to solve my problem.

I write this code:

currentBillTotal = Double.parseDouble(s.toString());

                temp=currentBillTotal*0.1;

                tip10EditText.setText(String.format("%.2f",temp));
                temp=currentBillTotal+temp;

                total10EditText.setText(String.format("%.2f",temp));

the String.format("%.2f",temp) did solve the issue of more than 2 digits after the dot..

thanks you all!

Gili Yaniv
  • 3,073
  • 2
  • 19
  • 34