简体   繁体   English

java.lang.NumberFormatException:空字符串 [FloatingDecimal]

[英]java.lang.NumberFormatException: empty String [FloatingDecimal]

I am getting the following error (java.lang.NumberFormatException: empty String).我收到以下错误(java.lang.NumberFormatException:空字符串)。

It runs normal when I type the number at the first time, but after I hit backspace on the last number left in the editText the activity closes down and gives the ERROR.当我第一次输入数字时它运行正常,但是在我在 editText 中留下的最后一个数字上按退格键后,活动关闭并给出错误。

I tried a lot but it did not work.我尝试了很多,但没有奏效。

THE MAINACTIVITY主要活动

package com.i.usvat;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class VatCal extends AppCompatActivity {

    TextView code, zipcode, Stax1, Ctax1, CUtax1, SPtax1, COMtax1, TPtax1;

    EditText STP, CTP, CUTP, SPTP, TTP, ProPrice1 ;

    private double ST;
    private double CT;
    private double UT;
    private double SP;
    private double T;
    private double PP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vat_cal);

        code = findViewById(R.id.code);
        zipcode = findViewById(R.id.zipcode);
        Stax1 = findViewById(R.id.Stax1);
        Ctax1 = findViewById(R.id.Ctax1);
        CUtax1 = findViewById(R.id.CUtax1);
        SPtax1 = findViewById(R.id.SPtax1);
        COMtax1 = findViewById(R.id.COMtax1);
        TPtax1 = findViewById(R.id.TPtax1);

        ProPrice1 = findViewById(R.id.ProPrice1);
        STP = findViewById(R.id.STP);
        CTP = findViewById(R.id.CTP);
        CUTP = findViewById(R.id.CUTP);
        SPTP = findViewById(R.id.SPTP);
        TTP = findViewById(R.id.TTP);


        code.setText(getIntent().getStringExtra("StateCode"));
        zipcode.setText(getIntent().getStringExtra("ZipCode"));
        Stax1.setText(getIntent().getStringExtra("Stax"));
        CUtax1.setText(getIntent().getStringExtra("CountyRate"));
        Ctax1.setText(getIntent().getStringExtra("CityRate"));
        SPtax1.setText(getIntent().getStringExtra("SpecialRate"));
        COMtax1.setText(getIntent().getStringExtra("COMRATE"));




        try {
            ProPrice1.setText("");
            calcResult();
        } catch (NumberFormatException e) {

            TextWatcher textWatcher = new TextWatcher() {
                public void afterTextChanged(Editable s) {
                    calcResult();
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
            };

            ProPrice1.addTextChangedListener(textWatcher);

        }
    }
    public void calcResult () throws NumberFormatException {

        String PR = ProPrice1.getText().toString();
        double value = Double.parseDouble(PR);
        String St = Stax1.getText().toString();
        double value1 = Double.parseDouble(St);

        ST = value * value1 / 100;
        String stringValue = Double.toString(ST);
        STP.setText(stringValue);
//

        String PR1 = ProPrice1.getText().toString();
        double value2 = Double.parseDouble(PR1);
        String Cut = CUtax1.getText().toString();
        double value3 = Double.parseDouble(Cut);

        UT = value2 * value3 / 100;
        String stringValue1 = Double.toString(UT);
        CTP.setText(stringValue1);
//
        String PR2 = ProPrice1.getText().toString();
        double value4 = Double.parseDouble(PR2);
        String Ct = Ctax1.getText().toString();
        double value5 = Double.parseDouble(Ct);

        CT = value4 * value5 / 100;
        String stringValue2 = Double.toString(CT);
        CUTP.setText(stringValue2);
//
        String PR3 = ProPrice1.getText().toString();
        double value6 = Double.parseDouble(PR3);
        String sp = SPtax1.getText().toString();
        double value7 = Double.parseDouble(sp);

        SP = value6 * value7 / 100;
        String stringValue3 = Double.toString(SP);
        SPTP.setText(stringValue3);
        //

        T = (value * value1 / 100) + (value2 * value3 / 100) + (value4 * value5 / 100) + (value6 * value7 / 100);
        T = Double.parseDouble(new DecimalFormat("#.###").format(T));
        String value11 = Double.toString(T);
        TTP.setText(value11);
        //

        String PRpp = ProPrice1.getText().toString();
        double valuepp = Double.parseDouble(PRpp);

        PP =  valuepp + T;
        String valueMAIN = Double.toString(PP);
        TPtax1.setText(valueMAIN);

    }
}


Screen recording:屏幕录制:

https://imgur.com/a/87SKTKu https://imgur.com/a/87SKTku

Every time when you get number value from an EditText you should use .trim() function to remove extra whitespaces like:每次从EditText获取数值时,都应该使用.trim() function 删除额外的空格,例如:

String PR = ProPrice1.getText().toString().trim();

You should also check that the taken string of numbers from an EditText is empty or not?您还应该检查从EditText获取的数字字符串是否为空? before converting it to a number variable like int , double or float as follows:在将其转换为intdoublefloat等数字变量之前,如下所示:

double value = PR.isEmpty() ? 0.0 : Double.parseDouble(PR);

If you do these checks then you will not get any NumberFormatException anymore for whitespaces.如果您进行这些检查,那么您将不会再收到任何NumberFormatException空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.lang.NumberFormatException:空字符串 FloatingDecimal 和 parseDouble - Exception in thread "main" java.lang.NumberFormatException: empty String FloatingDecimal & parseDouble java.lang.NumberFormatException:对于输入字符串:sun.misc.FloatingDecimal.readJavaFormatString处的“ 4 3 3 9 9 8 8 9” - java.lang.NumberFormatException: For input string: “4�⌷ 3⌷�9�⌷.�8⌷�9⌷” at sun.misc.FloatingDecimal.readJavaFormatString java.lang.NumberFormatException:Android 中的空字符串 - java.lang.NumberFormatException: empty String in Android java.lang.NumberFormatException: 空字符串 - java.lang.NumberFormatException: empty String java.lang.NumberFormatException:SentiWordNet中的空字符串 - java.lang.NumberFormatException: empty String in SentiWordNet 引起:java.lang.NumberFormatException:空字符串 - Caused by: java.lang.NumberFormatException: empty String JsonSyntaxException:java.lang.NumberFormatException:空字符串 - JsonSyntaxException: java.lang.NumberFormatException: empty String “java.lang.NumberFormatException:empty String”,非空字符串 - “java.lang.NumberFormatException: empty String” with a not empty String java.lang.numberformatexception空字符串java awt - java.lang.numberformatexception empty string java awt Java.lang.NumberFormatException错误:空字符串-Java应用程序 - Java.lang.NumberFormatException error: empty string- Java Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM