Question

The SeekBar on my tip calculator app crashes when I enter %0.02f as a parameter in the format method.

tipAmountEditText.setText(String.format("%0.02f", tipAmount));

I fixed this problem by removing the integer part thus becoming just %.02f. The only feature I can say about this issue is that it popped up using a ChangeListener. I do not understand why this would be an issue and I hope someone could enlighten me. If you need see the bigger picture, my code in its entirety is on my github: https://github.com/xamroc/TipCalc

private OnSeekBarChangeListener tipSeekBarListener = new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {

        tipAmount = (tipSeekBar.getProgress()) * .01;

        tipAmountEditText.setText(String.format("%.02f", tipAmount));

        updateTipAndFinalBill();

    }

};
Was it helpful?

Solution

Here String.format("%0.02f", tipAmount) you are getting

java.util.MissingFormatWidthException

//This Unchecked exception thrown when the format width is required.

Docs

Reason:

%0.02f interprets as a floating point at least 0 wide.

Thats why it gives MissingFormatWidthException // as its assuming width to 0

So use Following Instead

String.format("%.02f", tipAmount)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top