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.