我已经构建了一个带有一些数字的程序并用%打印它。但如果我输入的数字超过4位,程序就会崩溃。
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)+"");
}
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)
我该如何解决?为什么程序在4位数后崩溃?用更少的工作就可以了。
答案 0 :(得分:0)
NumberFormat
是本地化的类,而Double.parseDouble
则不是。{1}}。因此,在您的情况下,因为系统上的语言环境不是英语,所以使用了另一种格式,但Double.parseDouble
需要英语。 2种解决方法:
NumberFormat nf = NumberFormat.getInstance(Locale.EN_US)
创建您的数字格式 - 显然不是您想要的。nf.parse(st)
进行解析。这解析了本地化的字符串,应该更有用。 答案 1 :(得分:0)
我找到了解决问题的方法。
我写这段代码:
currentBillTotal = Double.parseDouble(s.toString());
temp=currentBillTotal*0.1;
tip10EditText.setText(String.format("%.2f",temp));
temp=currentBillTotal+temp;
total10EditText.setText(String.format("%.2f",temp));
String.format(“%。2f”,temp)确实解决了点后超过2位数的问题..
谢谢大家!