简体   繁体   中英

java.lang.NumberFormatException: empty String [FloatingDecimal]

I am getting the following error (java.lang.NumberFormatException: empty String).

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.

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

Every time when you get number value from an EditText you should use .trim() function to remove extra whitespaces like:

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

You should also check that the taken string of numbers from an EditText is empty or not? before converting it to a number variable like int , double or float as follows:

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

If you do these checks then you will not get any NumberFormatException anymore for whitespaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM