0

I'm creating a PV = nRT calculator that has the user enter any of the three given variables. Without the use of a button, the program will automatically detect that three variables have been given and will calculate the fourth missing variable automatically. This is my code so far:

    Thread calculate = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                if (Calculations.isCalcable(pressure.getText().toString(),
                        volume.getText().toString(),
                        moles.getText().toString(),
                        temperature.getText().toString()) == true) {
                    if (pressure.getText().toString().length() == 0) {
                        pressure.setText("1010");
                    } else if (volume.getText().toString().length() == 0) {
                        volume.setText("1010");
                    } else if (moles.getText().toString().length() == 0) {
                        moles.setText("1010");
                    } else {
                        temperature.setText("1010");
                    }


                }
            } catch (Exception e){

            }
        }
    });
    calculate.start();

This doesn't work. Also I'm not sure if I'm actually supposed to use a Thread or not. Here's what happens when I enter three variables (The R variable is a constant):

Imgur

In this case the Temperature text should've changed to "1010".

Here's my isCalcable class:

public class Calculations {
    public static boolean isCalcable (String pressure, String volume, String moles, String temperature){
        int hasNumber = 0;
        if (pressure.length() > 0){
            hasNumber++;
        }
        if (volume.length() > 0){
            hasNumber++;
        }
        if (moles.length() > 0){
            hasNumber++;
        }
        if (temperature.length() > 0){
            hasNumber++;
        }
        if (hasNumber == 3){
            return true;
        }
        return false;
    }
}

What do I do?

Gravitoid
  • 1,294
  • 1
  • 20
  • 20
xeno
  • 33
  • 4

1 Answers1

1

What you need is to detect whether value has been inputted on the text fields right? Then have you looked at TextWatcher? Here is an example on how to use it.

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //code
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        //code
    }

    @Override
    public void afterTextChanged(Editable s) {
        //code
    }
});

Basically you apply the TextWatcher to the three fields and watch. If all three fields have the values you need, then do the calculation.

Maybe you can add global variables to the activity and check these global variables on the onTextChanged() method of the text field, and if three of the variables are already complete, set the value of the fourth one.

Community
  • 1
  • 1
Keale
  • 3,924
  • 3
  • 29
  • 46
  • I already have a way to detect the value inputted in the text fields. I need the program to calculate the fourth variable after the other three variables are inputted WITHOUT the need for a button. – xeno Jul 02 '14 at 00:53
  • That's it. on the onTextChanged() method of each text field check if the three variables are not empty or not null then if they are not empty or null calculate and set the fourth variable. – Keale Jul 02 '14 at 01:05
  • Thanks! I'm a beginner so I'm not sure if a Thread is the correct thing to use for this. – xeno Jul 02 '14 at 01:34
  • 1
    You're welcome. If you have any more questions regarding this, comment again or post another question. Welcome to StackOverflow btw :D – Keale Jul 02 '14 at 02:19