Search code examples
javaandroidandroid-edittextcurrency

2 Edit text get currency format(###,###,###) and mines but (crash when clicked on btn mines)


here is the java code :

  package com.aliabbasi.mytamrin;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class full extends AppCompatActivity {
    EditText full_payment, cash_payment,remain_payment;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full);
        Toast.makeText(this, "مقادیر را با دقت وارد  کنید", Toast.LENGTH_LONG).show();
        Button btn_cancel = findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent btn_cancel = new Intent(full.this, MainActivity.class);
                startActivities(new Intent[]{btn_cancel});
            }


        });
       try {
            Button remain_check = findViewById(R.id.remain_btn);
            remain_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    full_payment = findViewById(R.id.edt_full_payment);
                    cash_payment = findViewById(R.id.edt_cash_payment);
                    remain_payment = (EditText) findViewById(R.id.edt_remain);
                    Float a = Float.parseFloat(full_payment.getText().toString());
                    Float b = Float.parseFloat(cash_payment.getText().toString());
                    Float s = a - b;
                    String r = Double.toString(s);
                    remain_payment.setText(r);
                }
            });

        } catch (Exception e) {
            Toast.makeText(this, "خطایی رح داده" + e, Toast.LENGTH_LONG).show();
        }
        try {
            EditText  editText= (EditText) findViewById(R.id.edt_full_payment);
            editText.addTextChangedListener(new MyNumberWatcher(editText));
        }
        catch (Exception e){
            Toast.makeText(this, "ERR"+e, Toast.LENGTH_LONG).show();
        }
        try {
            EditText  editText= (EditText) findViewById(R.id.edt_cash_payment);
            editText.addTextChangedListener(new MyNumberWatcher(editText));
        }
        catch (Exception e){
            Toast.makeText(this, "ERR"+e, Toast.LENGTH_LONG).show();
        }
        /*try {
            EditText  editText= (EditText) findViewById(R.id.edt_remain);
            editText.addTextChangedListener(new MyNumberWatcher(editText));
        }
        catch (Exception e){
            Toast.makeText(this, "ERR"+e, Toast.LENGTH_LONG).show();
        }*/

    }


}

i try the double variable there is no Error but program crashed when i press the remain_btn. i check the Debug and find out there is a problem with this: at com.aliabbasi.mytamrin.full$2.onClick(full.java:45). andNumberFormatException** but IDK how to solve it.** and i really like to know how fix this becase i stuck in this about 2 days... thanks for reading ... wish the best

code imag


Solution

  • You can do something like this. Remove that comma.

    Float a = Float.parseFloat(full_payment.getText().toString().replace(",", ""));