2

I've used addTextChangedListener(new TextWatcher() to remove leading zeroes however it only works in android soft keyboard numpad instead of my custom numpad. n0 is the zero button. basically whenever i hit the zero button, my app crashes.

    //custom zero button
            n0.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        input1.append("0");
                        operationA();
                }
            });    

I just need the code that the zero button will not return any value when clicked to avoid leading zeros. Below is the addTextChangedListener(new TextWatcher()

    input1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1 && s.toString().startsWith("0")) {
                    s.clear();
                }
            }
        });

Getting these error:

    AndroidRuntime: FATAL EXCEPTION: main
        Process: context, PID: 12408
        java.lang.NumberFormatException: empty String
            at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
            at java.lang.Double.valueOf(Double.java:511)
            at context.operationA(MainActivity.java:478)
            at context$17.onClick(MainActivity.java:383)
            at android.view.View.performClick(View.java:5610)
            at android.view.View$PerformClick.run(View.java:22265)
            at android.os.Handler.handleCallback(Handler.java:751)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6077)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

3 Answers 3

2

the error occurred when you try to parse string as double in operationA method but the String is Empty ,check if string is not Empty before convert it

Sign up to request clarification or add additional context in comments.

3 Comments

It still crashes :( that still works on android built in soft numpad but not my custom numpad zeron button.
updated thread with the error logs. replaced address "context" so it's not too long.
the error occurred when you try to parse string as double but the String is Empty
1

I had the same problem. I solved it by writing a function below:

private String trimLeadingZeros(String string)
{
    boolean startsWithZero = true;
    while (startsWithZero)
    {
        if(string.startsWith("0") && string.length()>=2 && !string.substring(1,2).equalsIgnoreCase("."))
        {string = string.substring(1);}
        else
        {startsWithZero = false;}
    }
    return  string;
}

This function also prevents from trimming all the zeros from a string like 00000.123 (it will return 0.123 instead of useless .123).

Comments

0

To remove leading zero in EditText, you can have a TextWatcher that detects the first typed-in zero, and clears it

TextWatcher watcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.toString().equals("0")) {
            editText.removeTextChangedListener(this);
            editText.setText("");
            editText.addTextChangedListener(this);
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

editText.addTextChangedListener(watcher);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.