C
C
Chesterfield252021-11-04 13:54:30
Java
Chesterfield25, 2021-11-04 13:54:30

How to make the right comparison?

There are 2 EditText in the application, I need to perform the mathematical action 1EditText * 2EditText and multiply by 2 and assign it to the text!
I found the implementation of what I need, but only when the value is removed from any EditText field, the application closes due to the fact that the field is empty, how can I fix this?

binding.inputPrice.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                int a = Integer.parseInt(binding.inputQuantity.getText().toString());
                int b = Integer.parseInt(binding.inputPrice.getText().toString());

                int res = (a * b) * 2;
                binding.totalPrice.setText(String.valueOf(res));

            }

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

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

        binding.inputQuantity.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                int a = Integer.parseInt(binding.inputQuantity.getText().toString());
                int b = Integer.parseInt(binding.inputPrice.getText().toString());

                int res = (a * b) * 2;
                binding.totalPrice.setText(String.valueOf(res));
            }

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

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

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Chesterfield25, 2021-11-04
@Chesterfield25

Here is what fixed my problem

private void gettingTotalPrice() {

        binding.inputPrice.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                try {
                    int a = Integer.parseInt(binding.inputQuantity.getText().toString());
                    int b = Integer.parseInt(binding.inputPrice.getText().toString());

                    int res = (a * b) * 2;
                    binding.totalPrice.setText(String.valueOf(res));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

            }

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

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

        binding.inputQuantity.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                try {
                    int a = Integer.parseInt(binding.inputQuantity.getText().toString());
                    int b = Integer.parseInt(binding.inputPrice.getText().toString());

                    int res = (a * b) * 2;
                    binding.totalPrice.setText(String.valueOf(res));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

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

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

J
Jacen11, 2021-11-04
@Jacen11

I'm sorry, but your questions are very stupid. You just don't pull programming in principle. You don't understand data types, exceptions, debugging, etc. You just don't know the alphabet, but you're trying to write a poem. These are elementary things that are studied at the very beginning. If you want to learn programming, start with things easier, if the task, then hire a freelancer.

int b = Integer.parseInt(binding.inputPrice.getText().toString());
already just from the description, I understand that the matter is in this line and the studio tells you exactly about it. And he even makes mistakes. An empty string cannot be a number! You can make int to Integer and then check for null, you can wrap it in a tri-catch. But you don't understand what I mean, do you?
Damn, you don't even understand that binding.inputQuantity.getText() and Editable s are the same

S
Sergey Vodakov, 2021-11-04
@WaterSmith

When your EditText contains an empty string, it turns out what you are doing . As a result, null is obtained . Since null cannot be assigned to the primitive type int , when you try to do this, you get a NullPointException - for the user it looks like the program just closed itself. You have two options: 1. Catch this NullPointException using try catch 2. Assign not to the primitive type int , but to an object Integer , and then after the assignment, check if the result is not equal to null Regardless of the method, either in catch or in
Integer.parseInt("");
else you reassign the value of the variable to 0, or you will throw an error, depending on what the logic of your program requires there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question