【问题标题】:Program crashes after String format when input is more than 4 digits当输入超过 4 位时,字符串格式后程序崩溃
【发布时间】:2025-11-30 08:45:01
【问题描述】:

我已经构建了一个程序,它需要一些数字并用 % 打印它。但是如果我输入超过 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)+"");
  }

日志猫-

 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 位数后崩溃?少一点就可以了。

【问题讨论】:

  • 发布崩溃的logcat。
  • 我添加它...我不知道如何使它可读...大声笑
  • java.lang.NumberFormatException: Invalid double: "1,111" 可能是因为你使用了逗号。解析器可能不喜欢这样。
  • 但 id 没有添加逗号...我只是在编辑文本中写了数字 1111。这可能是由于程序如何在 4 位数字后自动添加它?或者可能就像在控制台上那样。

标签: java android string double string-formatting


【解决方案1】:

NumberFormat 是本地化类,而 Double.parseDouble 不是。因此,在您的情况下,因为您系统上的语言环境不是英语,所以使用了另一种格式,但 Double.parseDouble 需要英语。两种解决方法:

  1. 使用NumberFormat nf = NumberFormat.getInstance(Locale.EN_US) 创建您的数字格式 - 显然不是您想要的。
  2. 使用nf.parse(st)进行解析。这会解析本地化的字符串,应该更有用。

【讨论】:

    【解决方案2】:

    我找到了解决问题的方法。

    我写了这段代码:

    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位的问题..

    谢谢大家!

    【讨论】: